2

Here is the piece of code, caused me issues:

mystr:
    .string "ABCDEFGH"
    .set mystrlen, . - mystr
.text 
.globl  main     
.type   main, @function 

main:
    sub $0x10, %rsp
    movq $0x44434241, 0x8(%rsp)
    lea 0x8(%rsp), %rcx

    movq $4, %rax
    movq $1, %rbx

    //movq $mystr, %rcx
    //movq $mystrlen, %rdx
    movq $4, %rdx
    int $0x80 
    movq $1, %rax
    movq $0, %rbx
    int $0x80

Represented fragment doesn't work, because while 64-bit addressing, registers for syscall arguments should be rdi, rsi, rdx to the 1st, 2nd and 3rd syscall arguments respectively, not rbx, rcx, rdx (as for call in x86-32). But I'm interested in why do the commented out lines work fine with rbx, rcx, rdx?

red0ct
  • 4,840
  • 3
  • 17
  • 44

1 Answers1

3

int $0x80 is the 32 bit compatibility interface which happens to work in 64 bit processes too. It of course uses the 32 bit convention, which is why your code works. Note that it is using only the low 32 bits of the registers, you were just lucky that your string was in low memory. Try the same with the string on the stack which is typically outside of 32 bit range and needs the 64 bit registers and thus the 64 bit convention.

Jester
  • 56,577
  • 4
  • 81
  • 125