CS1021 Tutorial 6
Reading and Writing to Memory
Q1 if a, b and c are 32bit signed integers stored at memory addresses 0x40000000, 0x40000004 and 0x40000008 respectively, write ARM assembly language instructions to compute:
Q2 If a, b and c are 256 bit integers stored at memory addresses 0x400000000, 0x40000020 and 0x40000040 respectively, write ARM assembly language instructions to compute:
(i) a = a | b (ii) c=a&b
;
; a 256-bit integer occupies 256/32 = 8 x 32-bit words ;
; R1 -> a
; R2 -> b
; R3 -> c ;
LDR R1, =0x40000000 LDR R2, =0x40000020 LDR R3, =0x40000040 LDR R4, =8
; R1 -> a
; R2 -> b
; R3 -> c
; cnt = 8 (8 words) ; R0 = a
; R5 = b
; R0 = a & b
; c = R0 = a & b
; R1 -> next word of a ; R2 -> next word of b ; R3 -> next word of c ; cnt -= 1 and set flags ; next word if cnt != 0
L LDR
LDR R5, [R2]
AND R0, R0, R5 STR R0, [R3] ADD R1, R1, #4 ADD R2, R2, #4 ADD R3, R3, #4 SUBS R4, R4, #1 BNE L
(iii) c=a+b
R0, [R1]
1
CS1021 Tutorial 6 2018 jones@scss.tcd.ie
(i) a = a + b (ii) c=a–b
LDR R0, =0x40000000 LDR R0, [R0]
LDR R1, =0x40000004 LDR R1, [R1]
SUB R0, R0, R1
LDR R1, =0x40000008 STR R0, [R1]
(iii) b=a*a
; R0 -> a
; R0 = a
; R1 -> b
; R1 = b
; R0 = a – b ; R1 -> c
; c = R0 = a – b
Q3 If a zero terminated string of ASCII characters is stored at memory address 0x40000000, write ARM assembly language instructions to count the number of characters in the string (not including the zero). For example, if the string of ASCII characters is 0x31 0x32 0x33 0x00, its length is 3.
; R0 = length ; R1 -> s
LDR R0, =0
LDR R1, =0x40000000
; length = 0
; R1 -> s
; load next byte of string ;0?
; finished if 0
; length += 1
; R1 -> next byte in s
; next byte
L LDRB
CMP R2, #0
BEQ L1
ADD R0, R0, #1 ADD R1, R1, #1 B L
L1
Q4 If a zero terminated string of ASCII characters is stored at memory address 0x40000000, write ARM assembly language instructions to store the string in reverse order at memory address 0x40001000. For example, if the string of ASCII characters is 0x31 0x32 0x33 0x00 the reverse string stored at memory location 0x40001000 is 0x33 0x32 0x31 0x00.
Q5 If a zero terminated string of ASCII characters is stored at memory address 0x40000000, write ARM assembly language instructions to reverse the string in situ (without using any other memory locations).
L3
;
ADD
R2, R2, #1 L
R2, R2, #1
B L1 SUB
;
; swap first and ;
L2 CMP
last bytes and work towards middle R1, R2 ;
R2, [R1]
; R1 -> first byte of string
; R2 -> last byte of string (excluding ;
; find last byte in string ;
terminating zero)
; R1 -> s
; R2 -> s
; load next byte of string ; 0?
;
; R1 -> next byte in s
; next byte
; R2 = R2 – 1
L
LDR R1, =0x40000000 MOV R2, R1
LDRB R0, [R2]
CMP R0, #0 BEQL1
BHS
LDRB
LDRB
STRB
STRB
ADD
SUB
BL2 ;
L3
R0, [R1]
R3, R2] ; R0, [R2] ; R3, [R1] ;
R1, R1, #1 R2, R2, #1
; R1 = R1 + 1
; R2 = R2 – 1
; if R1 >= R2 then finished
; swap bytes
2
CS1021 Tutorial 6 2018 jones@scss.tcd.ie