Sie sind auf Seite 1von 3

ECE 471

Microprocessor Applications Engineering


Stacks and Subroutines1
` Use a stack is to create temporary register
workspace for subroutines.
The ARM Assembly ` Any registers that are needed can be pushed onto the
stack at the start of the subroutine and
` popped off again at the end so as to restore them before
return to the caller: func1 func2
Dr. Yifeng Zhu STMFD :
: sp!,{regs,lr}
zhu@eece.maine.edu :
: :
:
BL func1 BL func2
:
:
Electrical and Computer Engineering :
:
: LDMFD
University of Maine MOV pc, lr
sp!,{regs,pc}

Fall 2009

1 The slides are derived from Workshop slides from ARM 2

Passing Parameters to Subroutines Passing Parameters to Subroutines


` Passing parameters in registers
` Passing parameters by reference
` Passing parameters on the stack

Saturating arithmetic means that if an


overflow occurs, the number is clamped
to the maximum possible value.

3 4
Standard ARM Calling Convention Example of Passing Parameters in Registers
` r15 is the program counter. SRAM_BASE EQU 0x40000000 saturate
` r14 is the link register. (The BL instruction, used in a AREA Passbyreg, CODE, READONLY
; subroutine saturate
; performs r2 = saturate32 (r0 << r1)
subroutine call, stores the return address in this ENTRY ; r0 = operand to be shifted
; r1 = shift amount m
register). LDR sp, =SRAM_BASE
; try out a positive case
; r2 = result
; r6 = scratch register
` r13 is chosen as the stack register. ; (this should saturate)
MOV r0, #0x40000000 STMIA sp!, {r6, lr}
` r12 is the Intra-Procedure-call scratch register. MOV r1, #8
BL saturate
MOV r6, #0x7FFFFFFF
MOV r2, r0, LSL r1
` r4 to r11: used to hold local variables. stop B stop ; if (
(r0
0 !
!= (
(r2
2 >> m))
))
` r0 to r3: used to hold argument values passed to TEQ r0, r2, ASR r1
; R2 = 0x7FFFFFFF^sign(r0)
a subroutine ... and also hold results returned EORNE r2, r6, r0, ASR #31

from a subroutine. LDMDB sp!, {r6, pc} ; return


END

5 6

Example of Passing Parameters in Reference Example of Passing Parameters in Stack


saturate saturate
SRAM_BASE EQU 0x40000000 ; subroutine saturate32 SRAM_BASE EQU 0x40000000
; r4 – results
; r3 – holds address of parameters ; r5 – operand to be shifted
AREA Passbymem, CODE, READONLY ; r4 – results AREA PassbyStack, CODE, READONLY ; r6 = scratch register
g
ENTRY ; r5 – operand to be shifted ENTRY
; r7 = shift amount m
; r6 = scratch register ; r4 = saturate32 (r5 << r7)
;stack pointers initated ; r7 = shift amount m : initialize the stack pointer
LDR sp, =SRAM_BASE ; r4 = saturate32 (r5 << r7) LDR sp, =SRAM_BASE STMIA sp!, {r3-r7, lr}
;writable mem for parameters ; try out a positive case
LDR r5, [sp, #-0x20]
LDR r3, =SRAM_BASE + 100 STMIA sp!, {r4-r7, lr} ; (this should saturate) LDR r7, [sp, #-0x1c]
LDMIA r3, {r5,r7} MOV r1, #0x40000000
MOV r6, #0x7FFFFFFF
MOV r1, #0x40000000 MOV r6, #0x7FFFFFFF MOV r2, #8 MOV r4, r5, LSL r7
MOV r2, #2 MOV r4, r5, LSL r7 ; push parameters on the stack
; if (r0 != (r2 >> m))
STMIA r3, {r1, r2} ; if (r0 != (r2 >> m)) STMIA sp!, {r1, r2} TEQ r5,
5 r4,
4 ASR r77
BL saturate TEQ r5, r4, ASR r7 BL saturate
; R2 = 0x7FFFFFFF^sign(r0)
; R2 = 0x7FFFFFFF^sign(r0) ; pop results off the stack EORNE r4, r6, r5, ASR #31
stop B stop EORNE r4, r6, r5, ASR #31 ; now r1 = result of shift
;move result to the stack bottom
STR r4, [r3]; move result to memory LDMDB sp!, {r1, r2} STR r4, [sp, #-0x20]
LDMDB sp!, {r4-r7, pc} ; return LDMDB sp!, {r3-r7, pc} ; return
stop B stop
END
END
7 8
Reference
` Wang Jianming, Lecture slides, Embedded System
Design, Institute of Information Science, Academia
Sinica Taiwan
Sinica,
` ARM, Guest Lecture Material, www.arm.com
` ARM System Developer’s Guide, Design and
Optimizing System Software, A. SLOSS, D. SYMES, and
C.WRIGHT, publisher: Elsevier
` ARM A Assembly
bl LLanguage, FFundamentals
d l anddT
Techniques,
h i
William Hohl, CRC Press
` The Definitive Guide to the ARM Cortex-M3, Joseph,
Yiu, Newnes Press
9