Assembly 자주 사용되는 정리

김선우·2022년 9월 13일
0

Microprocessors

목록 보기
7/7


<교안 6- 예제>


다음 중 SPAM_BASE, Main, strcopy, srcstr 은 그저 label일 뿐이다.
label is a name of memory location 이다.
1) SPAM_BASE EQU 0x40000000 ;address to store string
SPAM_BASE라고 라벨링한 이름에 EQU(equal to) 0x40000000을 할당했다.
2) AREA StrCopy, CODE
코드 블럭을 생성했다. Directives는 처음에 배운 포맷그대로 이므로, 그냥 넘어간다.
3) ENTRY
DIRECTIVES
4) Main adr r1,srcstr ;pointer to source string
-Main 이라고 labeling 했다.
-srcstr의 주소를 instruction의 개수에 따라 세보면 hexadecimal로 0x1c가 된다. 이러한 주솟값 0x1c가 register r1에 store 되었다.
5) ldr r0, =SPAM_BASE ;pointer to destination string
-,=는 중요한 syntax이다. r0에 SPAM_BASE라는 value가 담겨있다는 것이다.
6) ldrb r2,[r1], #1 ;load byte, update address
7) strb r2,[r0],#1 ;store byte, update address
8) cmp r2, #0 ;check for zero terminator
-cmp는 compare의 의미이다. (r2-0) set flag하고, 이는 branch 명령어에서 분기할지 말지를 정해야 하는 flag 이다.
9) bne strcopy ;loop if terminator not reached
-평소에는 loop을 주는데 이번에 strcopy로 labeling 했다.bne 명령어는 not equal to zero 라면 goes back 한다.
10) stop b stop
stop 하는 instruction
11) srcstr DCB "This is my (source) string",0
-T는 0x1c, h=0x10, i=0x1E,s는 0x1F의 주솟값을 가진다.이렇게 각각 하나하나 주솟값이 달라짐을 인지한다.
12) END ;end of file marker
<TUT4, week5의 예제3>



위의 코드를 한 줄 한 줄 분석해보자.
1) AREA Exercise3, CODE
Directives
2) ENTRY
Directives
3) ADR, r0, TABLE ;r0 is pointer to TABLE
ADR이 무슨의미일 까? address of label(Table) to register r0. r0의 TABLE의 주솟값을 담았다.

4) MOV r1, #6 ;r1 is data length(=counter)

5) MOV r2, #0 ; r2 is sum, initialized to 0

**6) loop LDR r3, [r0], #4 ;load TABLE value into r3,
;post-increment pointer
-LDR과 LDRSH의 차이:
LDR은 word를 담고, LDRSH는 signed half word를 담는다.

7)ADD r2,r2,r3 ; sum=sum+r3

8)SUBS r1,r1,#1 ;decrement counter

9)BNE loop ;loop if counter not equal 0

10)stop B stop
끝내는 instruction
11)Table DCD 0xFEBBA,0x1234,0x8888,0x13,0x8080808,0xFFFF
-위에서 부터, 0x24,0x28....쭉 간다.

12)END
Directives


1) MOV r0, #0 은 initial condition이다.
2) ADR r1, arrayb
array b는 address 이고, address 가 r1에 저장됨.
3)MOV r2, #SPAM_BASE
r2는 stored 0x40000000 value를 contain하고 있고 이는 address 값이다.

아래의 그림에서 blue color는 address이고, red color는 data stored in memory이다.

RSB STRB 명령어까지를 body라 한다.
RSB는 reverse subtract인데, RSB r3,r0,#7이란
r3=7-i이다.
첫 번째 loop 에서는 7-i가 7-0=7이 된다.

따라서, 처음 loop에서는 load에 b[7]이 된다.

profile
배고파...

0개의 댓글