#Help for adding numbers in assembly.

11 messages · Page 1 of 1 (latest)

open viper
#

I am a beginner and I am trying to make a small language transpile for a challenge and I cant seem to add two numbers.

vast flickerBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

open viper
#

I have the following code:

BITS 64
segment .text
dump:
    mov     r9, -3689348814741910323
    sub     rsp, 40
    mov     BYTE [rsp+31], 10
    lea     rcx, [rsp+30]
.L2:
    mov     rax, rdi
    lea     r8, [rsp+32]
    mul     r9
    mov     rax, rdi
    sub     r8, rcx
    shr     rdx, 3
    lea     rsi, [rdx+rdx*4]
    add     rsi, rsi
    sub     rax, rsi
    add     eax, 48
    mov     BYTE [rcx], al
    mov     rax, rdi
    mov     rdi, rdx
    mov     rdx, rcx
    sub     rcx, 1
    cmp     rax, 9
    ja      .L2
    lea     rax, [rsp+32]
    mov     edi, 1
    sub     rdx, rax
    xor     eax, eax
    lea     rsi, [rsp+32+rdx]
    mov     rdx, r8
    mov     rax, 1
    syscall
    add     rsp, 40
    ret

segment .text
global _start
_start:
    ;; -- push 255 --
    push 255
    ;; -- push 1 --
    push 1
    ;; -- pop --
    pop rax
    ;; -- dump --
    pop rdi
    call dump
    ;; -- push 255 --
    push 255
    ;; -- dump --
    pop rdi
    call dump
    ;; -- add --
    pop rax
    pop rbx
    add rax, rbx
    push rax
    ;; -- dump --
    pop rdi
    call dump
    mov rax, 60
    mov rdi, 0
    syscall
#

it was all generated using c

#

the dump and L2 were taken from c using godbolt

#

I get the following output:

255
255
140723262952527
#

I dont seem to know why 255 + 255 = 140723262952527??

torpid frost
#

You say you got the code from godbolt, but for what C code and with what compile options?

#

;asm -O1

int add(int a, int b) {
  return a + b;
}
dusky tigerBOT
#
Assembly Output
add:
  lea eax, [rdi+rsi]
  ret

vast flickerBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.