Programming/Assembly2012. 1. 23. 16:30


약 170억번 반복되는 반복문을 통하여 컴퓨터 성능을 시험해보는 예제 입니다.

반복문 시작 전 후로 현재 시간을 구하여 빼는 원리입니다.



TITLE Link Library Test #3 (TestLib3.asm)

; Calculate the elapsed time of executing a nested loop

INCLUDE Irvine32.inc

OUTER_LOOP_COUNT = 3 ; adjust for processor speed
.data
startTime DWORD ?
msg1 BYTE "Please wait...", 0dh, 0ah, 0 ; 0dh,0ah 는 개행문자입니다.
msg2 BYTE "Elapsed milliseconds: ",0
.code
main PROC
mov edx, OFFSET msg1 ; "Please wait..."이라는 문자열을 edx 에 넣습니다.
call WriteString ; 문자열 출력 - edx에 있는 주소의 문자열을 출력합니다.
; Save the starting time.
call GetMSeconds ; GetMSeconds - 자정 이후를 msec 단위로 계산하여 eax로 리턴
mov StartTime, eax ; eax 에 리턴된 값을 StartTime에 넣는다.
mov ecx, OUTER_LOOP_COUNT ; ecx - loop를 반복할 횟수를 넣어줌.

; Perform a busy loop.
L1:
call innerLoop ; innerLoop PROC 을 세번 호출한다.
loop L1

; Display the elapsed time.
call GetMSeconds ; 위의 루프가 끝나면 다시 한번 현재 시간을 구합니다
sub eax, startTIme ; 현재 시간에서 루프 시작하기 전 시간을 빼줍니다
mov edx, OFFSET msg2 ; "Elapsed milliseconds: "의 오프셋을 edx에 저장
call WriteString ; edx에 저장된 문자열 출력
call WriteDec ; eax에 있는 루프 도는데 걸린 시간 값을 출력
call Crlf ; 개행문자
exit ; main PROC 종료
main ENDP

innerLoop PROC ; innerLoop Proc 정의
push ecx ; 이전 루프에서 ecx가 사용되고 이번에도 사용되므로 스택에 저장함
mov ecx, 0FFFFFFFFh ; 반복 횟수를 FFFFFFFFh 로 지정
L1: ; L1 루프
mov eax, eax ; eax를 eax로 이동시키는 아무 의미 없는 코드 삽입
loop L1 ; L1 처음으로 . 
pop ecx ; 루프가 끝나면 이전 ecx를 스택에서 꺼내서 복원시킵니다
ret ; innerLoop Proc 을 리턴합니다.
innerLoop ENDP

END main

Posted by NullBr4in
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