0
votes

I know there are lots of references out there talking about NASM and mov but either I'm missing something fundamental or people need to write better help guides!

SECTION .data
    fmtStart:       db "Enter two numbers in format '# #'", 10, 0
    fmtTest:        db "sum: %d", 10, 0
    input:          db "%d %d", 0
SECTION .bss                ; BSS, uninitialized variables
    int1:       resd 1
    int2:       resd 1
    sum:        resd 1
SECTION .text               ; Code section.
    global main             ; the standard gcc entry point
    main:                   ; the program label for the entry point
    push ebp            ; set up stack frame
    mov ebp,esp

    ;; Get the data
    push dword fmtStart
    call printf
    add esp, 4

    push dword int2
    push dword int1
    push dword input
    call scanf
    add esp, 12

        ;; Do calculations
        ;; Add
            xor eax, eax
            mov eax, [int1]
            add eax, [int2]
            mov [sum], eax
        push dword sum
        push dword fmtTest
        call printf
        add esp, 24

    mov esp, ebp    ; take down stack frame
    pop ebp         ; same as "leave" op

    mov eax,0       ; normal, no error, return value
    ret                 ; return

I get:

Enter two numbers in format '# #'

2 3

sum: 4247592

which isn't what I get when I add 2 and 3 with my calculator, maybe that's just me though.

my understanding of the code is as follows: the data section declares variables that are initialized to stuff, in this case my formatted strings; the bss section is for uninitialized variables, in this case my input vars and the sum var; the text section is where the code goes; I declare main as the entry point for gcc; I prompt the user for two numbers; I zero out eax with the xor; move the value of int1 to eax; add the value of int2 to eax; move what's in eax to be the value of sum; push it onto the stack with the formatted string; call printf to display stuff; end the program.

--EDIT--

To be clear, either add isn't working or mov isn't working. It seems like add should be working so I'm assuming it's mov. I don't understand what about mov [var], register would be wrong but obviously something isn't right!

1
This is where you need to use your debugger. Are the values for sum1 and sum2 not being set correctly? Is the addition not working right for some reason? Or is the answer right but not being displayed correctly? (I think the problem is that push dword sum is pushing the address of the variable rather than its value.)Harry Johnston
printing int1 and int2 shows the proper values. I had the program working with immediately printing the value in eax, but now I want to use variables. If I print eax after the add it shows the correct value, the problem is definitely moving to the vardlkulp
No, the problem is with push dword sum. Try push dword [sum].Frank Kotler
yea, ok, I'm silly. I spent way to long messing with stuff and all I needed were two little square brackets!dlkulp

1 Answers

1
votes

Here's the problem:

    push dword sum
    push dword fmtTest
    call printf

printf, unlike scanf, takes its arguments (after the format) by value, while in your code sum is the address of the memory location. Just do:

    push [sum]
    push fmtTest
    call printf

(incidentally, the xor eax,eax before the mov eax,[int1] is useless, since you are immediately rewriting the content of the register)