A Few Tricks to Make Life Easier
Objectives
We would like to :
Understand the include statement for NASM
Understand basic (leaf) function calls.
Notes
I am tired of typing the constants over and over again.
A standard programming trick is include.
NASM supports this.
See
Chapter 5 of the NASM manual
%include "filename" (section 5.8.1 of the manual)
guards
%ifndef SOME_LABEL %define SOME_LABEL ;code %endif
See
CONSTANTS.h
Function Calls : Basics
Functions and function calls will be a issue for us all semester.
But I really want to use a function for printing numbers in the next code
So let's do a simple intro
We will not do things completely right here,
But it will work for now.
We can't use a jump because we don't know where to jump back to.
We use
call function_label
When done we use
ret
We will pass parameters the right way (rsi, rdi)
see
memory.asm
We may need to manage the stack
push rbp ; save the base pointer
move rbp, rsp ; make the current stack pointer the base pointer.
sub rsp,8 ; make the stack 16 bit alligned.
Once we are done, we need to reverse this process
We don't need to add to rsp, as we will be "clobbering it"
mov rsp, rbp ; restore the stack pointer
pop rbp ; restore the base pointer
ret ; return to the calling function