1

Here is my code which takes in two variables and sums them and subtracts them. Then, it prints the output. I have removed some of the functions to keep it simple:

_start:

    call _printfirstmsg
    call _getvar1
    call _printsecondmsg
    call _getvar2
    call _addvar1var2
    call _printsum
    call _subvar1var2
    call _printdiff
    call _dispvar1
    call _dispvar2
    
    mov rax, 60
    mov rdi, 0
    syscall
    
_printsum:
    mov rax, 1
    mov rdi, 1
    mov rsi,addi
    mov rdx, 4
    syscall
    
    ret
    
    
 _printdiff:
    mov rax, 1
    mov rdi, 1
    mov rsi,subt
    mov rdx, 4
    
    
    syscall
    ret
    


_addvar1var2:
   mov rax, [var1]
   add rax, [var2]
   mov [addi], rax
   syscall
   ret

 
_subvar1var2:
  mov rbx, [var1]
  sub rbx, [var2]
  mov [subt], rbx
  syscall
  ret
  

When I run the function, instead of returning the values of _printsum and _printdiff, it shows some unknown characters as outputs. Please tell me where I am mistaken.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0
_addvar1var2:
  mov rax, [var1]
  add rax, [var2]
  mov [addi], rax
  syscall
  ret

What do you think that the syscall does in this addition code? RAX is the sum of those 2 numbers and not a function number. Similar problem in the subtraction code.

...it shows some unknown characters as outputs

Your addi and subt variables contain numbers. Before outputting them with a function that expects characters, you must first convert them into text characters.

Read about one way how to display a number
Do read the comments below that question so that you can correct the code.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76