728x90
반응형
예제를 이용해서 공부해 보자
예제 1.
예제 2.cmp 를 이용해서 숫자를 비교한다그리고 그 결과를 가지고 점프를 사용하는데jnl: jump if not less 이상일떄SF=OFCF=0jnc: jump if not carry 캐리값이 없을떄이 조건들을 만족할시에만 점프가 가리키는 곳으로 점프를 하게 된다예외적으로jmp: 무조건 점프 가 있다점프를 시킬시에는 보통 elseLarge 와 같은 라벨을 만든다라벨을 만들시에는 중복이 될수도 있으니 차근차근 계획해서 만드는 것이 좋다
예제 3.jge jump if greater or equal SF=OF 비교한 값이 이상일떄(jnl 과 같다)jne jump if not equal ZF=0 같지 않을때점프를 수행하라
.386
.MODEL FLAT
INCLUDE io.h
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
CR EQU 0dh
Lf EQU 0ah
.STACK 4096
.DATA
prompt1 BYTE cr,Lf,Lf,"Player 1, please enter a number: ", 0
target DWORD ?
clear BYTE 24 DUP (Lf), 0
prompt2 BYTE cr,Lf,"Player 2, your guess? ", 0
stringIn BYTE 20 DUP (?)
lowOutput BYTE "too low", cr, Lf, 0
highOutput BYTE "too high", cr, Lf, 0
gotItOutput BYTE "you got it", cr, Lf, 0
countLabel BYTE Lf, "Number of guesses:"
countOut BYTE 6 DUP (?)
BYTE cr, Lf, Lf, Lf, "Do you want to play again? ",0
.CODE
_start:
untilDone: output prompt1
input stringIn,20
atod stringIn
mov target,eax
output clear
mov cx,0
untilMatch: inc cx
output prompt2
input stringIn,20
atod stringIn
cmp eax,target
jne ifLess
equal: output gotItOutput
jmp endCompare
ifLess: jnl isGreater
output lowOutput
jmp endCompare
isGreater: output highOutput
endCompare:
cmp eax,target
jne untilMatch
itoa countOut,cx
output countLabel
input stringIn,20
cmp stringIn,'n'
je endUntilDone
cmp stringIn,'N'
jne untilDone
endUntilDone:INVOKE ExitProcess, 0
PUBLIC _start
END
실행시
LOOP 에 대해서Player 1, please enter a number : 99Player 2, your guess? 22
too low
Player 2, your guess? 11
too low
Player 2, your guess? 33
too low
Player 2, your guess? 66
too low
Player 2, your guess? 44
too low
Player 2, your guess? 111
too high
Player 2, your guess? 100
too high
Player 2, your guess? 99
you got it
Number of guesses: 8
Do you want to play again? ;
Player 1, please enter a number: 11Player 2, your guess? 11
you got it
Number of guesses: 1
Do you want to play again? N
C:\MASM>이렇게 실행이 된다이 소스에서 특징을 보자면 숫자가 같을때까지 반복을 한다는 점인데untilDone: output prompt1
input stringIn,20
atod stringIn
mov target,eax
output clear
mov cx,0
untilDone 라벨을 만들어서 재시작이 가능하도록 하고
untilMatch: inc cx
output prompt2
input stringIn,20
atod stringIn
cmp eax,target
jne ifLessuntilMatch 라벨은얼마나 많은 숫자를 입력했는가를 보여주고cmp 를 이용 player1 이 입력한 숫자랑 player2 가 입력한 숫자를 비교하고jne 를 사용함으로서 같지않을시에는 무조건 ifLess 라벨로 이동하게하고같으면 바로 아래있는 equal 라벨이 실행된다
equal: output gotItOutput
jmp endCompareequal 라벨은 실행후 바로 endCompare 라벨로 이동하게 된다
ifLess: jnl isGreater
output lowOutput
jmp endCompare지금 현재 입력한 숫자들이 같지 않다는 것만 알고 있으니까그게 크거나 작다는 것을 알기 위해서jnl 을 이용 jnl은 작지않을시에만 점프를 하니까jnl 이 점프를 하였을 시에는 player2 가 입력한 숫자가 더 크다는 것을 알수있다
isGreater: output highOutput
endCompare:
cmp eax,target
jne untilMatch
itoa countOut,cx
output countLabel
input stringIn,20
cmp stringIn,'n'
je endUntilDone
cmp stringIn,'N'
jne untilDone여기서는 player1 이 입력한 숫자와 player2 가 입력한 숫자가 같지 않을떄에는 다시입력하도록 만들게 하고만약 같은숫자일 때에는 다시 할건지 물어본다음 끝낸다
endUntilDone:
INVOKE ExitProcess, 0
.DATA
prompt1 BYTE "-",CR,lf,0
.CODE
_start:
mov ecx,5
SMART:
output prompt1
loop SMART
prompt1 BYTE "-",CR,lf,0
.CODE
_start:
mov ecx,5
SMART:
output prompt1
loop SMART
INVOKE ExitProcess, 0
PUBLIC _start
END
_start 시작지점을 보면 ecx 에 5를 주고 있다 이 5는 loop 문을 돌면서 1씩 까이다가 0 이 되면서 프로그램이 종료가 될것이다
그리고 한번 실행될때마다 화면에는 prompt1 에 있는 - 라는것이 출력될 것이다
명령문 루프를 쓸떄에는loop statementLabel이런식으로 쓰면된다loop는 ecx 값과 밀접한 관계를 가지고 있는데ecx의 값이 0이 아닐시에만 반복문을 수행한다카운터 개념이라고 보면 된다이것은 곱셈의 MUL 과 eax와 edx 의 관계와 비슷하다그러므로 위의 쏘스를 실행하면 ECX 의 값이 5임에 따라 5번 구동하고 멈추게 되는 것이다조심할 점은 ECX 의 값에 0을 주지 않는것인데만약 0을 주게 될경우 LOOP 가 한번 구동됨에 따라 1이 빠진다그렇게 되면 ECX 의 값은 올 F 가 된다그렇게 되면 거의 무한반복문 수준의 반복을 하게 된다새로운 점프 명령어 jecxz 는 ecx 가 0일 시에는 점프를 하도록 해 주는것이다The jecxz instruction can be used to code a backward for loop when the loopbody is longer than 127 bytes, too large for the loop instructions single-byte displacement.5.5 ArraysPrograms frequently use arrays to store collections of data values. Loops are commonlyused to manipulate the data in arrays.ARRAY는 배열이다배열에서는 EDI 와 ESI 를 쓰는데 이것을 POINTER 개념으로 쓴다EDI 와 ESI 에서I=INDEXS=SOURCED=DESTINATION이다
파이프 의 구동 과정은 3가지 스텝이 있다
• fetch an instruction from memory 명령어를 가져오고• decode the instruction 명령어를 해독하고 레지스터 파일을 읽는다• execute the instruction 실행/주소 계산을 행한다파이프 명령어를 쓸떄 주의할 점은
점프명령어에 관해서인데
점프명령어는 점프 실행시 다른 위치로 넘어가서 실행을 하게 되는데
그 과정에서 파이프 명령어 다음번 줄의 명령어는 실행되는 도중에 다른 라인으로 이동해 버리기 때문에 제대로 된 연산이 수행되지 않는다
728x90
'코스웨어 > 14년 스마트컨트롤러' 카테고리의 다른 글
2014.08.06 업무보고 출석번호 6번 김용우 (10) | 2014.08.06 |
---|---|
2014.08.05 업무보고 출석번호 5번 김상엽 (7) | 2014.08.06 |
2014.08.04 업무일지 [김대희] (12) | 2014.08.04 |
2014.08.01 업무일지 [고한솔] (14) | 2014.08.01 |
2014.07.30. 업무일지 출석번호 22번 허수웅 (11) | 2014.07.31 |
2014.07.29 업무일지 출석번호21 이재우 (9) | 2014.07.30 |
(복사가능)Introduction to 80x86 Assembly Language and Computer Architecture.pdf (1) | 2014.07.29 |
07.28 - 업무일지 [20. 이경진] (11) | 2014.07.28 |