Search code examples
assemblyx86nasm

NASM - Adding two numbers - no output


I am trying to add 2 numbers and I don't get output for this program. Please help.

    section .text
    global _start

    _start:

    mov eax, 20
    mov ebx, 30
    add eax, ebx

    mov ecx, eax

    mov eax, 4
    mov ebx, 1
    int 80h

    mov eax, 1
    mov ebx, 0
    int 80h

Solution

  • As already said, you must convert the number to string and then to output this string:

    section .text
    global _start
    
    _start:
    
        mov  eax, 20
        mov  ebx, 30
        add  eax, ebx
    
        mov  edi, buffer
        mov  ecx, 10
        call _NumToStr
        xor  eax, eax
        stosb              ; null terminator
    
        mov  eax, 4
        mov  ebx, 1
        mov  ecx, buffer
        int  80h
    
        mov  eax, 1
        mov  ebx, 0
        int  80h        
    
    ; eax - number
    ; ecx - radix
    ; edi - buffer
    
    _NumToStr:
        test  eax,eax
        jns   _NumToStrU
        neg   eax
        mov   byte [edi],"-"
        inc   edi
    
    _NumToStrU
        cmp   eax,ecx
        jb    .lessA
    
        xor   edx,edx
        div   ecx
        push  edx
        call  _NumToStrU
        pop   eax
    
    .lessA:
        cmp   al, 10
        sbb   al, 69h
        das
        stosb
        ret
    
    section .bss
    
    buffer resb 64   ; reserve 64 bytes as buffer