Status: Tags: #cards/cmpt295/assembly Links: Coding MOC
x86-64 Assembly
Principles
Data
- 16 integer registers of 64 bits each: can manipulate either 1, 2, 4 or 8 bytes (memory address of 8 bytes as well) depending on the register name used
- Floating point registers of 64 bits each: can manipulate either 4 or 8 bytes depending on the register name used
- No aggregate types such as arrays or structures
Data management
Operations
- Memory referenence => data transfer instructions
- Transfer data from memory to register using mov*
- Load (mem → reg)
- Store (reg → mem)
- Move (reg → reg)
- Transfer data from memory to register using mov*
- Arithmetic and logical => data manipulation instructions
- Perform calculations
- ex) Arithmetic, logic, shift
- Perform calculations
- Branch and jump => program control instructions
- Transfer control
- Unconditional jumps to/from functions
- Unconditional/conditional branches
Syntax
- always include
.global {file_name}
in top - functions look like
|
|
$
is used for immediate/fixed value%
is used for referencing register- Putting () automatically refers to adress
- Don’t use it for non-memory adresses
- mov*
Function calls ;; call and ret
Parameters
Operands
Insructions
- One Operand Arithmetic Instructions
- Two Operand Arithmetic Instructions
- Two Operand Logical Instructions
- Two Operand Shift Instructions
testq ;; param & param
Operation sizes movb size of data transfer ;; 1 byte
movw size of data transfer ;; 2 bytes
movl size of data transfer ;; 4 bytes
movq size of data transfer ;; 8 bytes
mov src, dest ?
- copies variable info into a new one
cmov src, dest ?
- cmov is used to accompany cmp
- if cmpl, then cmov
- Not to be used in expensive, risky, or side effect computations
call
syntax: call func
where func
→ label (memory adress of first instruction of callee - function being called)
program counter: generic term that specifies next adress of instruction, denoted as %rip
(register instruction pointer)
-
stored in Memory Stack
-
when call executes:
- save value of program counter (PC → %rip) on stack
- pushq %rip
- return address, memory address of instruction after call func instruction
- pushq %rip
- set program counter (PC → %rip) to memory address of first instruction of func
movq "func, %rip
- Start executing func
jmp func
- save value of program counter (PC → %rip) on stack
ret
syntax: ret
- execution
- load return address from stack onto %rip
- popq %rip
- jump to return address (of caller) and start executing the instruction at that memory address
jmp %rip
- load return address from stack onto %rip
Example
Caller vs Callee and
multstore
Applies multiplication then stores value into dest
|
|
multstore in assembly
|
|
Multiplies 2 numbers
|
|
mult2 in assembly
|
|
Procedures
Backlinks
|
|
References:
Created:: 2022-01-31 14:47