0

I'm relatively new to assembly, and had written a simple program to print the numbers 1 to 10 to the screen. However, a short time after I wrote it, I realized I would need to convert it from x86 to x86_64 assembly for it to run on my main workstation.

Most of the conversion had gone rather well, except for one block, which printed the numbers, once they were converted to strings, on the screen.

In x86, the code to print was:

mov edx, eax
pop eax
mov ecx, eax
mov ebx, 1
mov eax, 4
int 80h

When I converted it to x86_64, I attempted to make it work by simply changing the register names.

mov rdx,rax
pop rax 
mov rcx, rax
mov rbx, 1
mov rax, 4
int 80h

In the program, rax holds the value of the ASCII number to print.

When the program is run now, in a 64-bit environment, no output is printed to the screen, and the program completes execution without a single error or message.

Is there any way to print strings to the screen in x86_64 assembly?

Thanks in advance!

ntrupin
  • 454
  • 8
  • 13

1 Answers1

2

I just want to say thanks a ton to fuz. Sadly, it turns out my question was completely irrelevant, and the bug was in a piece of code that came slightly after the block, where the write call was made.

When he told me to show my full program, I did a quick look over the code, and found the issue.

Previously, I had attempted to print using:

mov rdx,rax
pop rax 
mov rcx, rax
mov rbx, 1
mov rax, 4
int 80h

However, this did not work, and since it was not throwing any errors I assumed the bug was in the code right before it, the block from the question, which regulated passage to the print block. The correct code looks like this:

mov rdx,rax
pop rax
mov rsi, rax
mov rdi, 1
mov rax, 1
int 80h

Again, thanks a ton, and apologies for my lack of proper assembly bug-checking!

ntrupin
  • 454
  • 8
  • 13
  • The problem code should be in the question, not the answer. You should also edit the title to match the actual problem, so that users searching for similar problems will find it. – Barmar Jul 11 '19 at 21:17
  • I will do that ASAP! – ntrupin Jul 11 '19 at 21:22
  • Yup, bad args to a system call never raises an exception / signal, it returns `-EFAULT` or `-EINVAL`. This is why you use `strace` when debugging. – Peter Cordes Jul 11 '19 at 23:39
  • I'd never heard of `strace` before! I'm definitely going to use that in the future. Thanks! – ntrupin Jul 12 '19 at 00:05
  • You should not use `int 80h` to perform system calls in 64 bit mode. This is because these system calls do not support 64 bit arguments. Instead, use the `syscall` instruction. Note that the system call numbers and calling convention are different for that. – fuz Jul 12 '19 at 00:45