240 likes | 381 Views
ECE 447: Lecture 20. Stack Operations. ECE 447: Stack Operations. Push and pull. N Z V C. PSH [A, B] PSH [X, Y] PUL [A, B] PUL [X, Y]. INH. – – – –. Initialize and store stack pointer. N Z V C. LDS. 0 –. IMM, DIR, EXT, IND. STS. DIR, EXT, IND. 0 –. ECE 447: Stack Operations.
E N D
ECE 447: Lecture 20 Stack Operations
ECE 447: Stack Operations Push and pull N Z V C PSH [A, B] PSH [X, Y] PUL [A, B] PUL [X, Y] INH – – – – Initialize and store stack pointer N Z V C LDS 0 – IMM, DIR, EXT, IND STS DIR, EXT, IND 0 –
ECE 447: Stack Operations Subroutine call and return from subroutine N Z V C – – – – REL BSR JSR DIR, EXT, IND – – – – RTS – – – – INH
ECE 447: Stack Operations Jump JMP DIR, EXT, IND – – – – Stack pointer manipulation INH – – – – DES INS INH – – – – TS [X, Y] INH – – – – T [X, Y] S – – – – INH
ECE 447: Stack after a function call 0 SP after a function call RTNH SP before a function call RTNL MAX_ADDRESS
ECE 447: Stack after an interrupt call SP after an interrupt CC B 0 A IXH IXL IYH IYL RTNH RTNL SP before an interrupt MAX_ADDRESS
arg_2 arg_2 ... ... arg_N arg_N Using the stack - Entering a function with a fixednumber of arguments and local variables caller function called function SP D var_1 IX arg_1 Local variables ... or D IX DES or PSHX var_M arg_1H arg_1L reg_1 reg_1 ... ... Saved registers PSH reg_L reg_L TSX RTNH RTNH RTNH JSR or BSR Return address RTNL RTNL RTNL arg_2 arg_2 ... ... Function arguments PSH arg_N arg_N SP
arg_2 arg_2 ... ... arg_N arg_N ECE447: switch Statements caller function called function D SP ret var_1 IX or D IX Local variables ... retH retL var_M reg_1 reg_1 INS or PUL ... ... Saved registers PUL reg_L reg_L PULY INS RTNH RTNH RTNH Return address RTNL RTNL RTNL arg_2 ... Function arguments JMP 0,Y arg_N SP
arg_1 arg_1 ... ... arg_N arg_N Using the stack - Entering a function with a variablenumber of arguments caller function called function SP var_1 IX Local variables ... DES or PSHX var_M reg_1 reg_1 ... ... Saved registers PSH reg_L reg_L TSX RTNH RTNH RTNH JSR or BSR Return address RTNL RTNL RTNL arg_1 arg_1 ... ... Function arguments PSH arg_N arg_N SP
arg_1 arg_1 ... ... arg_N arg_N Using the stack - Exiting a function“caller-cleanup” convention caller function called function SP D var_1 IX ret Local variables ... or TXS D IX var_M retL retH reg_1 reg_1 INS or PULX ... ... Saved registers PUL reg_L reg_L RTNH RTNH RTNH Return address RTNL RTNL RTNL RTS arg_1 arg_1 ... ... Function arguments PULz arg_N arg_N SP
main.c /* C version of main function */ #include <stdio.h> extern long multadd(unsigned int a, unsigned char b, long c, long *pm); long product, sum; void main(void) { sum = multadd(258, 17, 32L, &product); printf("product = %ld\nsum = %ld", product, sum); }
multadd.c /* C version of the multadd function */ long multadd(unsigned int a, unsigned char b, long c, long *pm) /* this function returns a*b + c and writes a*b to the location of memory pointed out by pm */ { long tmul; long tadd; tmul = a*b; *pm = tmul; tadd = tmul + c; return tadd; }
amain.s11 (1) ; assembly language version of the main function .global multadd .global printf section .bss Product rmb 4 sum rmb 4 section .data format fcc 'product = %ld',$0a,'sum = %ld',$00
amain.s11 (2) section .text main: ; pushing arguments arg4 to arg2 to the stack ; arg4: pm = &product ldx #product pshx ; arg3: c = 32L = 0x00000020 ldx #$0020 pshx ldx #$0000 pshx ; arg2: b = 17 = 0x11 ldaa #$11 psha ; storing argument arg1 in the register D ; arg1: a = 258 = 0x102 ldd #$102 jsr multadd
amain.s11 (3) ; storing result in the sum variable stx sum std sum+2 ; push arg3, sum, to the stack ldx sum+2 pshx ldx sum pshx ; push arg2, product, to the stack ldx product+2 pshx ldx product pshx ; storing argument arg1 in the register D ldx #format xgdx ; call function jsr printf
amain.s11 (4) ; deallocate argument space pulx pulx pulx pulx pula pulx pulx pulx rts end
amultadd.s11 (1) ; assembly language version of the multadd function tmul_offset EQU 0 tadd_offset EQU 4 a_offset EQU 8 b_offset EQU 12 c_offset EQU 13 pm_offset EQU 17
amultadd.s11 (2) section .text multadd: ;--------------------------------------------------------- ; Initialization sequence, similar for different functions ;--------------------------------------------------------- ; storing D register pshb psha ; reserving space for two local variables ; long tadd pshx pshx ; long tmul pshx pshx ; IX set to point to the top of the stack tsx
amultadd.s11 (3a) ;--------------------------------------------------------- ; Part specific for the given function ;--------------------------------------------------------- ; Multiplication ;--------------------------------------------------------- ; tmul = a*b = (a_high * 2^8 + a_low) * b ; ACCB = b ldab b_offset,X ; ACCA = LSB of a = a_low ldaa a_offset+1,X ; ACCD = b*a_low mul ; tmul = b*a_low std tmul_offset+2,X ldd #0 std tmul_offset,X ; ACCB = b ldab b_offset,X ; ACCA = MSB of a = a_high ldaa a_offset,X
amultadd.s11 (3b) ; ACCD = b*a_high mul ; tmul= b*a_low + (2^8)*b*a_high addd tmul_offset+1,X std tmul_offset+1,X ; *pm = tmul ldy pm_offset,X ldd tmul_offset,X std 0,Y ldd tmul_offset+2,X std 2,Y
amultadd.s11 (4a) ;--------------------------------------------------------- ; Addition ;--------------------------------------------------------- ; tadd = c ldd c_offset,X std tadd_offset,X ldd c_offset+2,X std tadd_offset+2,X ; tadd = tadd + tmul pshX clc ldab #4 loop ldaa tadd_offset+3,X adca tmul_offset+3,X staa tadd_offset+3,X dex decb bne loop pulx ; End of part specific for the given function
amultadd.s11 (5) ;--------------------------------------------------------- ; Exit sequence ;--------------------------------------------------------- ; restore value of the stack pointer txs ; return long result in registers IX and D ; IX:D = tadd ldx tadd_offset,X ldd tadd_offset+2,X ; deallocate space for local variables ; repeat this 8 times ins ; remove register D from the stack puly ; store return address in register IY puly
amultadd.s11 (6) ; repeat 7 times ins jmp 0,Y end
SP multadd main 0 IX xx xx tmul xx xx 4 xx xx tadd D xx LDD $0102 xx 8 $01 $01 D=a $02 $02 RTNH RTNH RTNH PSHX PSHX PSHX PSHX RTNL RTNL RTNL 12 b $11 $11 $11 $11 13 $00 $00 $00 $00 TSX $00 $00 $00 $00 c PSHD $00 $00 $00 $00 LDX PSHX JSR $20 $20 $20 $20 17 pm &product &product &product &product SP