Exercise #5
This exercise is mostly more of the same of exercise #4. Convert the .c
files to X86_64 assembly code. Once again to do this I recommend copying
the .c
file to the .s
file and begin converting the code line by line.
And again:
-
It is not recommended that you use the syscall registers:
rax
,rdi
,rsi
,rdx
,r10
,r8
orr9
(orr11
) for your main program except for system calls, userbx
orr12
-r15
. Preferably use memory for variable storage. -
The "command line parameters" to your assembly program are placed on the stack for you by the kernel. They are located immediately before where the stack register (
rsp
) points to. The first parameter is "argc
", the value of which would be at[rsp]
.argv
is then "above" that location, each pointer is 8 bytes, thusargv[0]
is at[rsp+8]
,argv[1]
at[rsp+16]
, etc. If the value at[rsp+n*8]
== 0, then you are at the end of the arguments list.To access the first character of
argv[1]
, placing it inr15
would then require:
mov r14, [rsp+16] ; loads address of argv[1] into r14. Remember that
; argv[1] is a pointer (i.e. an address) to the
; string, it must still be de-referenced.
mov r15, BYTE [r14] ; Move the byte at the address in r14 into r15.
Refer to exercise #4 for the argv.s
code to access the command line
parameters.
Furthermore the library is in its own sub-directory, so use 'lib/lib.h' as path to the header file in your .s files if you wish to use the library and its functions in your program.