Exercise #3
Convert the ascii.c
file to X86_64 assembly code (in ascii.s
). To do this I
recommend copying the ascii.c
file as ascii.s
and begin converting the code
line by line. You may also wish to do this in iterative fashion, such as in
the following steps:
-
It is not recommended that your program use the following registers for anything other than making system calls:
rax
,rdi
,rsi
,rdx
,r10
,r8
orr9
(i.e. the registers used for system calls,) norr11
(which seems to be used by the kernel as well.) Consider only usingrbx
orr12
-r15
. -
Make the function to print a character (
printchar
). Place a label calledprintchar
at the beginning of the function, then make thewrite
system call. Assume the character value to be printed is in thebl
register, store that value in some memory location that has been set aside for the character data using theMOV
instruction. The write system call number (1
) should be placed inrax
, the descriptor number (1
=stdout
) should be placed in therdi
register, the address of the memory holding the character value should be placed inrsi
and the length (1
) should be placed inrdx
. After the registers have been setup, make theSYSCALL
, thenRET
(return) from theCALL
'ed function.
printnum:
; store bl in memory
; setup registers for the write syscall
; make the syscall
; return from function.
; elsewhere in the program (say _start or printascii, etc):
; set bl with the character to be printed
; CALL printnum
; do more stuff...
-
Test the
printchar
function by printing the ASCII values from 32 to 126. -
Make the
printnum
function. Test it by first printing one number, then test it by printing all the numbers from 0 to 127. -
Make the
printascii
function and test it with a specific ASCII value. -
Complete the
_start
routine with code equivalent to what is inmain()
in the C program.