13.1.1 An Example

Let us consider an example. Let's say that we want to write a subroutine to print one character to the standard output file. This can be rather handy so that we don't have to set up the system service every time.

Let's call this subroutine putchar, it may be implemented as follows:

.data
localbuf: .fill 1
.text
putchar:
    push  %eax
    push  %ebx
    push  %ecx
    push  %edx
    movl  $4,%eax
    movl  $1,%ebx
    movl  $localbuf,%ecx
    movl  $1,%edx
    int   $0x80
    pop   %edx
    pop   %ecx
    pop   %ebx
    pop   %eax
    ret

In this implementation, we use a ``global'' variable localbuf to pass information from the caller to the subroutine. In other words, if we want to print the character J, we can do the following:

    movb  $'J',localbuf
    call  putchar

This works, but it has a few problems.

Copyright © 2009-04-16 by Tak Auyeung