ARM汇编代写代做代考 CS1021 Tutorial 8 Stacks and Subroutines

CS1021 Tutorial 8 Stacks and Subroutines
Q1 If SP = 0x40010000, R4 = 4, R5 =5 and R6 = 6 (1) draw a diagram of the stack after the following instructions are executed and (2) what are the contents of R4, R5, and R6?
CS1021 Tutorial 8  2018 jones@scss.tcd.ie
(i) PUSH {R4} PUSH {R5}
POP
{R6}
no RAM
no RAM
4
5
SP
0x40010004 0x40010000 0x4000FFFC 0x4000FFF8 0x4000FFF4
0x40010004 0x40010000 0x4000FFFC 0x4000FFF8 0x4000FFF4
0x40010004 0x40010000 0x4000FFFC 0x4000FFF8 0x4000FFF4
R4 = 4, R5 = 5, R6 = 5
(ii) PUSH POP
SP
{R4, R5} {R4, R5}
no RAM
no RAM
5
4
(iii) PUSH POP
{R4, R5} {R5, R4}
SP
R4 = 4, R5 = 5, R6 = 6
no RAM
no RAM
5
4
R4 = 4, R5 = 5, R6 = 6
1


… … …


(iv) PUSH POP
POP POP
{R4, R5, R6} {R6}
{R5}
{R4}
(v) PUSH POP
POP
{R4, R5, R6} {R6, R4} {R5}
CS1021 Tutorial 8  2018 jones@scss.tcd.ie
no RAM
no RAM
6
5
4
SP
0x40010004 0x40010000 0x4000FFFC 0x4000FFF8 0x4000FFF4 0x4000FFF0
0x40010004 0x40010000 0x4000FFFC 0x4000FFF8 0x4000FFF4 0x4000FFF0
R4 = 6, R5 = 5, R6 = 4
no RAM
no RAM
6
5
4
SP
R4 = 4, R5 = 6, R6 = 5
Q2 If SP = 0x40010000 and R4 = 0, what do the following instructions do?
PUSH {R4} ; PC = 0 (branch to 0) POP {PC}
Q3 Write suitable entry and exit code for a leaf subroutine XXXX which modifies R4, R5, R6 and R7.
XXXX PUSH {R4, R5, R6, R7} ; push R4, R5, R6 and R7 …;
…;
POP {R4, R5, R6, R7} ; pop R4, R5, R6 and R7 BX LR ; return
Q4 Write suitable entry and exit code for a non-leaf subroutine YYYY which modifies R4, R5, and R7.
YYYY PUSH {R4, R5, R7, LR} ; push R4, R5, R7 and return address …;
…;
POP {R4, R5, R7, PC} ; pop R4, R5, R7 and return
2
… … … …
… …

CS1021 Tutorial 8  2018 jones@scss.tcd.ie Q5 Write a subroutine STRLEN which returns the length of NUL terminated ASCII string in R0. The
address of the string is passed to the subroutine in R0.
; R1-> str
;R0=0
; R2 = ch AND R1 = R1 + 1 ; ch == 0?
; finished
;R0=R0+1
; next ch
; return
;
; leaf subroutine ;
STRLEN MOV
STRLEN1 BX
LR
R1, R0 MOV R0,#0
STRLEN0 LDR
CMP R2, #0
BEQ STRLEN1 ADD R0, R0, #1 B STRLEN0
R2, [R1], #1
Q6 Write a subroutine LEN that computes √𝑥2 + 𝑦2. Assume x is passed to the subroutine in R0, y in R1 and that the result is returned in R0. Assume also that you can call a subroutine SQRT which the returns the integer square root of R0 in R0.
If a is stored @ 0x40000000, b @ 0x40000004 and c @ 0x40000008 respectively, write code, using subroutine LEN, to compute 𝑐 = √𝑎2 + 𝑏2.
; push return address ;R2=x*x
;R0=y*y ;R0=x*x+y*y
; R0 = sqrt(x*x + y*y) ; return
; R4 -> a (use R4 as it will not be modified by LEN) ; R0 = a AND R4 -> b
; R1 = b AND R4 -> c
; R0 = sqrt(a*a + b*b)
; c = sqrt(a*a + b*b)
;
; non-leaf subroutine (calls SQRT) ;
LEN PUSH
{LR}
MUL R2, R0, R0
MUL R0, R1, R1 ADD R0, R2, R0 BL SQRT
POP {PC}
….
MAIN LDR
LDR R0, [R4], #4
R4, =0x40000000
LDR R1, [R4], #4 BL LEN
STR R0, [R4]
3