shellhead
shellhead

Reputation: 153

Stack Frame in Assembly

I'm taking an assembly course and I'm having issues with the stack frame. One of the requirements is to pass parameters by using the stack rather than registers. I'm attempting to pass a variable by reference and then change the value of that variable in the procedure. Here's what I have thus far. WriteString and ReadDec are from a (Irvine) library that came with the book.

.data
numVal  DWORD ?
.code
main PROC
    PUSH    OFFSET numVal
    CALL    GetNumber
    exit
main ENDP
GetNumber PROC
    PUSH    edx
    PUSH    ebp
    MOV     ebp, esp

    CALL    ReadDec                ; gets what the user inputs and puts in eax
    MOV     [ebp+12], eax
    ; MOV    numVal, eax           ; this works just fine

    POP     ebp
    POP     edx
    RET     4
GetNumber ENDP
END main

However, if I try to print out or use numVal at some other point, it comes back as 0, regardless of what is entered by the user. I'm assuming it's sending whatever the user enters to ebp+12, which is referring to that address rather than numVal. So, if that is correct, is there a way to do this by passing numVal by reference or otherwise? Or is my commented out line, MOV numVal, eax the only way to do it?

PS. This is homework, I tried to tag it as such but it didn't allow me to.

PPS. This is also my first time posting on overflow, I'd be more than grateful for any constructive criticism to improve posting, etiquette, etc.

Upvotes: 4

Views: 1424

Answers (1)

Frank Kotler
Frank Kotler

Reputation: 3119

You need to "dereference" the parameter passed.

GetNumber PROC
    PUSH    edx
    PUSH    ebp
    MOV     ebp, esp

    CALL    ReadDec                ; gets what the user inputs and puts in eax
    MOV     edx, [ebp+12] ; edx now has "offset numVal"
    mov     [edx], eax ; put result of ReadDec there
    ; MOV    numVal, eax           ; this works just fine

    POP     ebp
    POP     edx
    RET     4
GetNumber ENDP

I wouldn't have pushed edx - at least not where you did - but it "should" work. Untested!

Upvotes: 4

Related Questions