Take the 2-minute tour ×

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

share|improve this question
We are talking intel right? –  Tom Oct 29 '09 at 3:00
we are talking at&t... –  KJP Oct 29 '09 at 3:05
Intel or AT&T syntax aside (it makes no difference), Tom is asking if you are talking about Intel x86 assembly. –  ephemient Oct 29 '09 at 4:11
add comment

3 Answers

up vote 6 down vote accepted

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
        movl    $1, %ebx
        lea     str, %ecx
        movl    $len, %edx
        movl    $write, %eax
        int     $0x80
        xorl    %ebx, %ebx
        movl    $exit, %eax
        int     $0x80
str:    .ascii "Hello, world!\n"
len = . -str
.globl  _start
$ as -o hwa.o hwa.S
$ ld hwa.o
$ ./a.out
Hello, world!
share|improve this answer
What? That takes ten clock cycles for the pusha and popa. The eight independent pushes and pops take one each. If you're going to get down and dirty with assembly, you've got to keep the faith and run at max speed :-) –  paxdiablo Oct 29 '09 at 4:45
In any case, you could just create your own system call that used eax for the code and ebx for the address of a null-terminated string. Voila (although you'll need to convince Linus et al to get it into the mainstream kernel) - let me know how that goes :-) –  paxdiablo Oct 29 '09 at 4:49
+1 for a good answer by the way. –  paxdiablo Oct 29 '09 at 4:50
Heh, si, but 10 cycles@3 GHz, I'll wait :-) –  DigitalRoss Oct 29 '09 at 4:54
where do you get your clock measurements? –  Nathan Fellman Oct 29 '09 at 15:27
add comment

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

share|improve this answer
add comment

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

share|improve this answer
can you give me an example? –  KJP Oct 29 '09 at 2:57
add comment

Your Answer


