Sum 10 numbers and print result in NASM

13,253

Solution 1

I see a couple of issues here. First, you've got num1 and total declared as dw. dw may sound like it means "dword", but it means "data word". You want these to be dd - "data dword"... since that's how you're using them. (and add ebx, 4 not 2) If you really need to use word (16-bit) values in 32-bit code, it can be done, but is awkward.

The second problem I see is that push total before your call _printf pushes the address of total. You want the "[contents]" of memory here, so push dword [total]. (push msg is correct)

After this, you probably want add esp, 8 (I like to write it as add esp, 4 * 2 - two parameters of 4 bytes each). It is possible to "defer" this stack cleanup - mov esp, ebp will fix you up, but it needs to be done BEFORE the pop ebp!!!

... there may be more...

Solution 2

You are using 32-bit memory access to work with 16-bit values.

Each element of the array is 16 bits wide. However, since eax is 32 bits wide, the following treats the array as if each element was also 32 bits wide:

add eax,[ebx]

The following also isn't quite correct, for exactly the same reason:

mov [total],eax

I think there's also an issue whereby you're pushing the address of total before calling printf(), whereas you should probably be pushing its value.

Also, as pointed out by @JasonD, you need to clean up the stack after calling printf().

Finally, the

mov esp,ebp

instruction should be removed as all it does is corrupt your stack pointer.

Solution 3

dw defines 16-bit entities but add eax,[ebx] is adding 32-bit entities. Either change dw to dd or put WORD PTR before [ebx]. Also, as NPE points out, you need to change how you handle storing eax into total.

Share:
13,253
santosh singh
Author by

santosh singh

Just another developer in languages C#, SQL,Jest, Jquery 😊 https://www.codeguru.co.in Javascript testing using jest

Updated on October 23, 2022

Comments

  • santosh singh
    santosh singh over 1 year

    I have written following assembly code for adding 10 numbers.I am able to compile it and execute it but I am getting wrong result. I just wanted to know how do I print the value of total on scree.

    section .data
    num1: dw 10, 20, 30, 40, 50, 10, 20, 30, 40, 50
    total: dw 0
    msg :  db "sum=%d",10,0
    
    section .text
        extern _printf
        global _main
    _main:
        push ebp
        mov ebp,esp
        mov ebx,num1 ;point bx to first number
        mov ecx,10      ;load count of numbers in ecx
        mov eax,0       ;initialize sum to zero
    loop:
        add eax,[ebx]
        add ebx,2
        sub ecx,1
        jnz loop
        mov [total],eax
    
        push total
        push msg
        call _printf
    
        pop ebp
        mov esp,ebp
        ret
    

    solution

    section .data
    num1: dd 10, 20, 30, 40, 50, 10, 20, 30, 40, 50,300
    total: dd 0
    msg :  dd "sum=%d",10,0
    
        section .text
            extern _printf
            global _main
        _main:
            push ebp
            mov ebp,esp
            mov ebx,num1 ;point bx to first number
            mov ecx,11     ;load count of numbers in ecx
            mov eax,0       ;initialize sum to zero
        loop:
            add eax,[ebx]
            add ebx,4
            sub ecx,1
            jnz loop
            mov  [total],eax
            push dword [total]
    
            push msg
            call _printf
            mov esp,ebp
            pop ebp
    
            ret
    
  • NPE
    NPE over 11 years
    @JasonD: Oh yeah, I forgot about that. I'll add a note to the answer. Thanks.