Question

Is there really no way to print an ascii string in assembly to standard output without using up all four general purpose registers?

Was it helpful?

Solution

Right, it takes three registers for the parameters plus one for the system call number...

But, x86 has pusha and popa, which will push and pop all the registers in one instruction.

$ cat hwa.S
write = 0x04
exit  = 0xfc
.text
_start:
        pusha
        movl    $1, %ebx
        lea     str, %ecx
        movl    $len, %edx
        movl    $write, %eax
        int     $0x80
        popa
        xorl    %ebx, %ebx
        movl    $exit, %eax
        int     $0x80
.data
str:    .ascii "Hello, world!\n"
len = . -str
.globl  _start
$ as -o hwa.o hwa.S
$ ld hwa.o
$ ./a.out
Hello, world!

OTHER TIPS

Well.. If you linked against libc you can call puts, then you'd have some callee-save registers... :-)

But yeah. The syscall interface is pass-by-register. Sorry.

Don't be so shocked. It'd be the same way if you were doing a function call on some calling conventions. For many platforms that's pretty standard. (Including all amd64 compilers I know of...)

You could write a function that takes the needed arguments from the stack.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top