0

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
itsme
  • 5
  • 1
  • 5
  • System call 4 writes strings, not integers. You'll have to convert your result to a string first, or use the `printf` function from libc. – Michael Jul 16 '13 at 05:33
  • @Michael I am trying to stick to a strict assembly code. So I don't want to use printf. Any other way? – itsme Jul 16 '13 at 06:48
  • Yeah, convert the integer to a string by repeatedly dividing by 10 and saving the remainder + '0' in a buffer in backwards order. Then print that string. – Michael Jul 16 '13 at 07:12

1 Answers1

0

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
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
johnfound
  • 6,857
  • 4
  • 31
  • 60
  • thanks john. The code has lot of terms that I don't know. I am a beginner here. Any way I have to find what test, xor and push does. – itsme Jul 16 '13 at 07:43
  • [Intel's software developer's manual](http://download.intel.com/products/processor/manual/325462.pdf) covers that. – Michael Jul 16 '13 at 07:44