Domanda

I am trying to write a simple sum function in x86 assembly - to which i am passing 3 and 8 as arguments. However, the code doesn't print the sum. Appreciate any help in spotting the errors. I'm using NASM

section .text
global _start

_sum:
    push ebp
    mov ebp, esp
    push edi
    push esi           ;prologue ends
    mov eax, [ebp+8]
    add eax, [ebp+12]
    pop esi            ;epilogue begins
    pop edi
    mov esp, ebp
    pop ebp
    ret 8

_start:
    push 8
    push 3
    call _sum
    mov edx, 1
    mov ecx, eax
    mov ebx, 1      ;stdout
    mov eax, 4      ;write
    int 0x80
    mov ebx, 0
    mov eax, 1      ;exit
    int 0x80
È stato utile?

Soluzione

To me, this looks like Linux assembler. From this page, in the Examples section, subsection int 0x80, it looks like ecx expects the address of the string:

_start:
    movl $4, %eax   ; use the write syscall
    movl $1, %ebx   ; write to stdout
    movl $msg, %ecx ; use string "Hello World"
    movl $12, %edx  ; write 12 characters
    int $0x80       ; make syscall

So, you'll have to get a spare chunk of memory, convert your result to a string, probably null-terminate that string, and then call the write with the address of the string in ecx.

For an example of how to convert an integer to a string see Printing an Int (or Int to String) You'll have to store each digit in a string instead of printing it, and null-terminate it. Then you can print the string.

Sorry, I have not programmed in assembly in years, so I cannot give you a more detailed answer, but hope that this will be enough to point you in the right direction.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top