KJP
KJP

Reputation: 541

Linux assembly woes

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

Upvotes: 0

Views: 286

Answers (3)

DigitalRoss
DigitalRoss

Reputation: 146241

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!

Upvotes: 6

Brian
Brian

Reputation: 1822

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

Upvotes: 1

asveikau
asveikau

Reputation: 40274

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...)

Upvotes: 1

Related Questions