0

I am novice in assembly, I have one question. Why I don't get anything in terminal when I run it? To compile it I write (nasm -felf64 example.asm -o example.o && ld example.o -o example && ./example)

section .data
   first_number:
           dq '50'
   second_number:
           dq '40'
   
   section .text
   global _start
   _start: 
          mov rax, first_number
          mov rdi, second_number
          cmp rax, rdi
          jae .true
          jb .false
  
  .true:  
          mov rcx, ‘1’
          jmp .execution
  .false: 
          mov rcx, ‘0‘
          jmp .execution
  .execution: 
          mov rax, 1
          mov rdi, 1
          mov rsi, [rcx]
          mov rdx, 8
          syscall
          
          mov rax, 60
          xor rdi, rdi
          syscall

But why if I change rsp on rcx this code won’t work? Thank you

Ruslan
  • 51
  • 5
  • [please remove the line numbers from your code](https://meta.stackexchange.com/q/7119/230282) – phuclv May 30 '21 at 12:10
  • 1
    Using `rsp` as a general purpose register is not good. – ecm May 30 '21 at 12:14
  • Why is using rsp not good idea? – Ruslan May 30 '21 at 12:16
  • 1
    Because it's the stack pointer. But that's not the reason for no output. The `write` system call expects an address and you pass 0 or 1 which are invalid. Also you compare 8 bytes when you want to compare 2. – Jester May 30 '21 at 12:23
  • RSP isn't quite general purpose so you can't use it for arithmetic in most situations [Can I use rsp as a general purpose register?](https://stackoverflow.com/q/22211227/995714) – phuclv May 30 '21 at 12:27
  • I have written mov rsi, [rsp] but it have not changed anything – Ruslan May 30 '21 at 12:28
  • What is register I can use for that? – Ruslan May 30 '21 at 12:31
  • Since `rsp` is 0 or 1 `[rsp]` is just as invalid. You need to put your stuff into memory and pass its address. Also note that ascii code `0` and `1` are nonprintable. If you want to see something you should use `'0'` and `'1'` – Jester May 30 '21 at 12:45
  • You need to write your character that you want output into memory. – Jester May 30 '21 at 12:58
  • It’s 1 or 0.It depends on result – Ruslan May 30 '21 at 12:58
  • 2
    Instead of `mov rsp, '0'` do `push '0'` and instead of `mov rsi, [rsp]` do `mov rsi, rsp`. – Jester May 30 '21 at 12:59
  • As I suppose I should get 1,right? – Ruslan May 30 '21 at 13:02
  • 1
    Also you should use brackets to access the values. You are comparing the addresses. Do `mov rax, [first_number]`. Then you will get 1 output, yes. Note you only need to print 1 character not 8. – Jester May 30 '21 at 13:06

0 Answers0