본문 바로가기
코스웨어/14년 스마트컨트롤러

20140731 업무일지 출석번호 1번 고윤석

by 알 수 없는 사용자 2014. 7. 31.
728x90
반응형
예제를 이용해서 공부해 보자

예제 1.

         



cmp 를 이용해서 숫자를 비교한다
그리고 그 결과를 가지고 점프를 사용하는데

jnl: jump if not less     이상일떄
SF=OF
jnc: jump if not carry        캐리값이 없을떄
CF=0
이 조건들을 만족할시에만 점프가 가리키는 곳으로 점프를 하게 된다
예외적으로
jmp: 무조건 점프 가 있다

점프를 시킬시에는 보통 elseLarge 와 같은 라벨을 만든다
라벨을 만들시에는 중복이 될수도 있으니 차근차근 계획해서 만드는 것이 좋다

예제 2.


jge jump if greater or equal SF=OF          비교한 값이 이상일떄(jnl 과 같다)
jne jump if not equal           ZF=0             같지 않을때
점프를 수행하라

예제 3.

.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                    

실행시
Player 1, please enter a number : 99
 
Player 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: 11

Player 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     ifLess

untilMatch  라벨은
얼마나 많은 숫자를 입력했는가를 보여주고
cmp 를 이용 player1 이 입력한 숫자랑 player2 가 입력한 숫자를 비교하고
jne 를 사용함으로서 같지않을시에는 무조건 ifLess 라벨로 이동하게하고
같으면 바로 아래있는 equal 라벨이 실행된다

equal:          output     gotItOutput
          jmp     endCompare

equal 라벨은 실행후 바로 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 


LOOP 에 대해서

.DATA
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 loop
body is longer than 127 bytes, too large for the loop instructions single-byte displacement.
 


5.5 Arrays
Programs frequently use arrays to store collections of data values. Loops are commonly
used to manipulate the data in arrays.

ARRAY는 배열이다
배열에서는 EDI 와 ESI 를 쓰는데 이것을 POINTER 개념으로 쓴다
EDI 와 ESI 에서
I=INDEX
S=SOURCE
D=DESTINATION
이다


              
파이프 의 구동 과정은 3가지 스텝이 있다
• fetch an instruction from memory        명령어를 가져오고
• decode the instruction                        명령어를 해독하고 레지스터 파일을 읽는다
• execute the instruction                        실행/주소 계산을 행한다

파이프 명령어를 쓸떄 주의할 점은

점프명령어에 관해서인데

점프명령어는 점프 실행시 다른 위치로 넘어가서 실행을 하게 되는데

그 과정에서 파이프 명령어 다음번 줄의 명령어는 실행되는 도중에 다른 라인으로 이동해 버리기 때문에 제대로 된 연산이 수행되지 않는다



example07311.asm

배열 관련 소스



728x90