Programming/Assembly2012. 1. 23. 16:02


책에서 보다가 괜찮은 샘플소스가 있어서 첨부해봅니다.

전경색과 배경색을 바꾸고

배열을 16진수로 출력하고

부호 있는 정수에 대해 입력을 받아서

입력한 정수를 10진수, 16진수, 2진수로 출력하는 샘플 코드 입니다.




TITLE Library Test #1: Integer I/O (TestLib1.asm)

;Tests the Clrscr, Crlf, DumpMem, ReadInt,
;SetTextColor, WaitMsg, WriteBin, WriteHex,
; and WriteString procedures.

Include Irvine32.inc
.data
arrayD DWORD 1000h, 2000h, 3000h                ; DWORD 형 크기가 3인 배열 arrayD 생성
prompt1 BYTE "Enter a 32-bit signed integer:",0 ; 문자열 생성
dwordVal DWORD ?                                      ; DWORD 형 변수 dwordVal 생성

.code
main PROC
; Set text color to yellow text on blue background:
mov eax, yellow + (blue * 16)              ; 배경색을 blue로, 글씨색을 yellow로 설정하겠다는 것을 eax에 입력
call SetTextColor                               ; eax값을 인자로 가져가는 SetTextColor 함수 호출로 설정
call Clrscr ; 화면 초기화

; Display the array using DumpMem.
mov esi, OFFSET arrayD ; arrayD의 오프셋을 esi에 저장
mov ecx, LENGTHOF arrayD ; arrayD의 길이를 ecx에 저장
mov ebx, TYPE arrayD ; arrayD의 타입, 즉 DWORD를 ebx에 저장(4가 저장됩니다.)
call DumpMem ; memory 출력. 위에서 입력한 esi,ecx,ebx를 기준으로 출력합니다.
                                                                ; 함수들에 대해 자세한 내용은 따로 포스팅 하겠습니다. 
call Crlf ; 개행

; Ask the user to input a signed decimal integer.
mov edx, OFFSET prompt1                  ; "Enter a 32-bit signed integer: " 문자열의 오프셋을 edx에 저장
call WriteString                                   ; edx에 있는 문자열 출력 
call ReadInt ; integer 입력 받기
mov dwordVal, eax ; ReadInt는 eax로 받은 값이 리턴되기 때문에 받은 값을 dwordVal 변수에 저장

; Display the integer in decimal, hexadecimal, and binary.
call Crlf ; new line
call WriteInt ; 부호 있는 10진수로 출력
call Crlf
call WriteHex ; 부호 있는 16진수로 출력
call Crlf
call WriteBin ; 부호 있는 2진수로 출력
call Crlf
call WaitMsg ; "Press any key..." 문자열 출력 후 키 입력 대기

; Return console window to default colors.
mov eax, lightGray + (black * 16)         ; 작업이 끝났으면 실행창의 환경설정을 다시 돌려줍니다.
call SetTextColor                               ; 바로 위에서 적용한 검은색 바탕에 밝은 회색으로 설정
call Clrscr ; 화면 초기화
exit
main ENDP
END main

Posted by NullBr4in