Lesson 1:
Notes:
Not really a language
Basically Machine code
Has more control over the processor
More code to write
Code made for cpu architecture
x86 cpus have registers
like working memory
Have random access
Different purpose registers
Have a fixed width
64 -bit or 32-bit
64-bit can run 32-bit but 64-bit cannot on 32-bit
Has these things called Stacks
Stacks is a more permanent way of storing memory
like stacking books
Dysplayed as an array
Stack Pointer(like the register)
Have random access
Can alter the top of the stack
Syntax can differ from assembler to assembler
Even on the same architecture
Operations
Syntax: operation [operands, ...]
mov
Moves a value into a register
Syntax
register, value/register
add
Adds a value to a register
Syntax
register, value/register
sub
Subtracts a value from a register
Syntax
register, value/register
int
Interrupts the process
Syntax
Interrupt instruction written in hexadecimal
mul
Multiplies a value into eax/rax(32-bit/64-bit)
Syntax
value
div
divides eax/rax(32-bit/64-bit) by a value
Syntax
value
Note this value is the right hand operator
Code
To tell the compiler what to your code it you can use the section .data and section .text
.data is for things like variables and structs
.text is where you put your assembly code
Lesson 2:
Notes:
Instruction Pointer
This value is where the code in the code the cpu is processing
You can't assign this a value by the operations above
You have to use something called jump ops
Can be used for conditional operations like if x = y do z if not do a
For this you create a label and you can use the syntax jmp label to jump to a label in your code(refer to jump.asm in the repository)
For this example in the repo I use cmp all that means is that im comparing one value to another one
The point above brings the next point you can use cmp to make conditionals like if ecx is equal to 4 then run x code if not run y
Operations
jmp
jumps to a label
Syntax:
jmp label
cmp
compares to values
cmp value/register(mostly a register), value/register
Note: Everything below is used after cmp
je
see if the values are equal if so it will jump to a label
Syntax:
je label
jne
same as above but if they are not equal
jne
Syntax:
jne label
jg
jumps if the value is greater
Syntax:
jg label
jge
jumps if value is greater or equal
Syntax:
jge
jge label
jl
jumps if value is less
Syntax:
jl label
jle
jumps if less than or equal
Syntax:
jle label
Lesson 3:
Notes:
Addresses
Used to store information
you can edit this code in the runtime
To do this do mov [variable] byte "a" edit the first byte
add a + then a number to move the position
Data types
db = 1 byte
dw = 2 bytes
dd = 4 bytes
The Stack (LIFO(Last in First Out))
Putting in something is like puting a book onto a stack
Popping is like taking that book off
Can use the pointer to tell where this is meaning its random access
In 32 bit the stack it stores 4 bytes in each point
Each stack entry is in 4s
Starts at the highest and works down
Can use the example about to work with the stack
Operations
push
push a value to the stack
Syntax
push value
pop
moves the top value off the stack to the desination defined
Syntax
pop register/variable
Lesson 4:
Notes:
Functions
Uses the operation call
pushes the instruction pointer onto the stack
Next instruction
Jumps to the location we are calling
A combination allows us to jump to code and then jump back without hard coding it in
To return back you can use the operation ret
returns the function back to the value of eip/rip(32 bit/64 bit) that was stored in the stack
Operations
call
call a function
Syntax
call label_name
ret
returns to the value of eip/rip(32 bit/64 bit) that as pushed to the stack
Syntax
ret
Lesson 5:
Notes:
Functions Again
Arguments
Arguments are pushed to the stack in Assembly
You have to add 8 to the stack allowing you to access these arguments then after that if you have more than one you would add 4 to esp/rsp(32 Bit / 64 Bit)
The value returned from a function is stored in the eax/rax(32 bit / 64 Bit) register
To call functions from C just put extern and then name the function there
To link this code it is best to use gcc instead of ld
The reason is that gcc is used to compile C code so it will have the all the C libraries to use
Operations
extern
Links an external function
Syntax
extern function_name