Stack Data Structure: Local Variables
We want to compute (for some reason) the formula \(a^2 + 2b + bc\). The variables \(a\), \(b\) and \(c\) are stored on the stack. After the computation these variables are removed from the stack and the result was pushed on the stack.
Complete the following code snippet:
.equ ZERO, 0 .equ RIP, 1 .equ RSP, 3 .equ TMP1, 7 .equ TMP2, 8 .equ TMP3, 9 .macro stack_init orq %ZERO, %ZERO, %RSP .endm .macro pushq REG subq $8, %RSP, %RSP movq \REG, (%RSP) .endm .macro popq REG movq (%RSP), \REG addq $8, %RSP, %RSP .endm .macro set VAL, REG orq \VAL, %ZERO, \REG .endm .macro enter VAL set \VAL, %TMP1 pushq %TMP1 .endm .text stack_init # enter a enter $1 # enter b enter $2 # enter c enter $3 # now we have stored a, b and c on the stack: # a in 16(%RSP) # b in 8(%RSP) # c in (%RSP) # compute a^2 + 2*b + b*c # a*a -> res # youre code here # 2*a -> res # youre code here # b*c halt