Docsity

Stack Operations and Function Calls in Assembly and C: A Comparative Study - Prof. Krzyszt, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Stack Operations and Function Calls in Assembly and C: A Comparative Study - Prof. Krzyszt and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

ECE 447 - Lecture 12

Stack operations

Push and pull

INH

N Z V C

  • โ€“ โ€“ โ€“

PSH [A, B]

PSH [X, Y]

PUL [A, B]

PUL [X, Y]

IMM, DIR, EXT, IND

N Z V C

Initialize and store stack pointer

DIR, EXT, IND 0 โ€“

LDS

STS

Subroutine call and return from subroutine

BSR

JSR

REL

N Z V C

  • โ€“ โ€“ โ€“

DIR, EXT, IND โ€“ โ€“ โ€“ โ€“

RTS INH โ€“ โ€“ โ€“ โ€“

Jump

Stack pointer manipulation

JMP DIR, EXT, IND โ€“ โ€“ โ€“ โ€“

DES

TS [X, Y]

T [X, Y] S

INH

INH

INH

  • โ€“ โ€“ โ€“
  • โ€“ โ€“ โ€“
  • โ€“ โ€“ โ€“

INS

INH โ€“ โ€“ โ€“ โ€“

arg_

arg_N

arg_

arg_N

arg_

arg_N

arg_

arg_N

RTNH

RTNL

RTNH

RTNL

RTNH

RTNL

Saved registers

var_

var_M

caller

function

called

function

SP

SP

IX

arg_

D

arg_1L

D

arg_1H

IY

or

PSHz

JSR

or BSR

PSHz

DES

or PSHX TSX

Local variables

Return address

Function arguments

reg_

reg_L

reg_

reg_L

Using the stack - Entering a function

LDD

LDY

arg_

arg_N

arg_

arg_N

arg_

arg_N

RTNH

RTNL

RTNH

RTNL

RTNH

RTNL

Saved registers

var_

var_M

caller

function

called

function

SP

SP

IX

ret

D

retL

D

retH

IY

or

JMP 0,X

PULX

INS

PULz

INS

or PULX

TXS

Local variables

Return address

Function arguments

reg_

reg_L

reg_

reg_L

LDD

LDY

Using the stack - Exiting a function

โ€œreturn-and-deallocateโ€ convention, __pascal

; assembly language version of the main function

import multadd import printf

section .bss product ds.l 1 sum ds.l 1

section .strings format dc.b โ€™product = %ldโ€™,$0a,โ€™sum = %ldโ€™,$

section .data dc.l 0.

amain.s11 (1)

section .text main: ; pushing arguments arg4 to arg2 to the stack ; arg4: pm = &product ldx #product pshx ; arg3: c = 32L = 0x ldx #$ pshx ldx #$ pshx ; arg2: b = 17 = 0x ldaa #$ psha

; storing argument arg1 in the register D ; arg1: a = 258 = 0x ldd #$

jsr multadd

amain.s11 (2)

; storing result in the sum variable sty sum std sum+

; push arg3, sum, to the stack ldx sum+ pshx ldx sum pshx ; push arg2, product, to the stack ldx product+ pshx ldx product pshx ; push arg1, #format, to the stack ldx #format pshx ; call function jsr printf

amain.s11 (3)

; deallocate argument space pulx pulx pulx pulx pulx

rts end

amain.s11 (4)

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

; Exit sequence ;---------------------------------------------------------

; restore value of the stack pointer txs

; return long result in registers IY and D ; IY:D = tadd ldy tadd_offset,X ldd tadd_offset+2,X

; deallocate space for local variables repeat 4 pulx

; remove register D from the stack pulx

; store return address in register IX pulx

amultadd.s11 (5)

repeat 7 ins

jmp 0,X

end

amultadd.s11 (6)

RTNH

RTNL

main multadd

SP

SP

IX

D

LDX

PSHX

JSR

PSHD

PSHX

PSHX

PSHX

PSHX

TSX

&product

LDD

&product

RTNH

RTNL

&product

RTNH

RTNL

&product

xx xx

xx

xx

xx

xx

xx

xx

tmul

tadd

D=a

b

c

pm