0% found this document useful (0 votes)
9 views27 pages

Understanding Assembly Language Basics

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views27 pages

Understanding Assembly Language Basics

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assembly Language

Syntax of Assembly Statements


what is stored in memory
register names
Branch instruction
function calls
The stack
Recursive function
Levels of Programming Languages

High-Level Languages (HLL)

Easy for humans to read/write (e.g., C, Python, Java).


Abstracted from hardware details.
Require a compiler to translate into machine code.

Low-Level Languages
Assembly Language
Uses mnemonics like ADD, SUB, MOV.
Closer to hardware, but human-readable.
Needs an assembler.

Machine Language
The binary code (0s and 1s) directly executed by CPU.
Very fast but hard for humans to write.
Assembly language
A low-level programming language where each instruction
corresponds directly to a machine instruction.

Why
Humans understand natural languages (English, Russian, etc.).
With training, humans can also understand high-level languages (C,
Java).
But a computer only understands binary (0s and 1s).
Direct control of CPU and memory
High efficiency and performance
Useful for debugging and embedded systems

Example:
add x5, x6, x7 # x5 = x6 + x7

The syntax of assembly code depends on the assembler being used.


Different assemblers (e.g., GNU Assembler, NASM, MASM) may have slightly different formats.
The basic instructions and operand formats remain mostly the same.
we focus on GNU Assembler
Part of GNU Compiler Collection (GCC).
Assembly Program Structure (RISC-V)

1 .file – Names the source file (useful for debugging)


.file "program.s"

2 .data – Holds initialized variables / constants


.data
num: .word 10
msg: .asciiz "Hello"

3 .text – Holds actual instructions (code section)


.text
.globl _start
_start:
li a0, 5
li a1, 7
add a2, a0, a1 # a2 = a0 + a1
Syntax of Assembly Statements

[label] mnemonic [operands] [; comment]

Label → Optional, marks a location in code


Useful for loops, functions, and branch targets.

Mnemonic → The operation (instruction) to perform.


Examples: add,mov, sub, mul, ld, st.

Operands → Data/parameters for the instruction


< instruction > < destination register > < operandregister1 > < operandregister2 >

Comment → Explanation (ignored by assembler)


Example:
loop: add x5, x6, x7 ; x5 = x6 + x7
what is stored in memory
Text (Code Section)

Stores program instructions (machine code).

Data Section
Stores initialized global/static variables.
Example: int x = 5;

BSS Section
Stores uninitialized global/static variables.
Example: int y;

Heap
Used for dynamic memory allocation (malloc, new).
Grows upward as the program requests memory.

Stack
Stores local variables, return addresses, function frames.
Grows downward (from high address → low address).
Very important for function calls & recursion.
simple assembly code

add r3, r1, r2


In this ARM assembly statement, the add instruction is specifying the fact that we wish to add two
numbers and save the result in some pre-specified location.

The name of the instruction is add, the destination register is r3, the operand registers are r1 and r2. The
detailed steps of the instruction are as follows:

1. Read the value of register r1.


2. Read the value of register r2.
3. Compute r3 = r1 + r2.
4. Save v3 in register r3
Branch instruction
conditional branching

Unconditional branching
function calls
simple function call

#call

#return

A function (or procedure) is just a block of code you can call and later return from.
In assembly, there’s no "function" keyword — instead, the method of calling and returning is handled by
instructions and conventions.
Function Call = Jump + Save Return Address
function calling conventions
input arguments and return vaule
When jal ra, doubleValue runs, it overwrites ra (the return address back to main).
After doubleValue returns, ra no longer points to main.
returns to the wrong place.
The stack

To store return addresses when functions are called.


To store local variables of functions.
To save register values that must be restored later.
To support recursion, where multiple copies of the same function may run at the same time.
How function use the stack
Storing register vaules on the stack
The stack during function call

sp

Preserved registers
storing saved registers on the stack
Leaf Function
A function that does NOT call any other function.

Non-Leaf Function
A function that calls another function inside it.
Needs to save registers (especially ra = return Non-leaf function calls
address) before calling another function.
eaxmple
stack during function calls

Stack now:
Stack after f1 pushes its registers:

Address | Content
Address | Content
----------------------
----------------------
f2_sp + 0 | s4 <- f2's saved s4
sp + 16 | a0
f1_sp +16 | a0
sp + 12 | a1
f1_sp +12 | a1
sp + 8 | ra <- return address to main
f1_sp + 8 | ra
sp + 4 | s4
f1_sp + 4 | s4
sp + 0 | s5
f1_sp + 0 | s5
Top of stack (sp now points here)
Top of stack (sp points to f2's s4)

Return is LIFO: last called function is first to pop.


Both f1 and f2 share the same stack, but each has its own frame.
Recursive function
example

return 1
recursive function
stack during recursive function

SP3+4 : [ n = 1 ]
SP3+0 : [ RA_return_to_f2 ]
SP2+4 : [ n = 2 ]
SP2+0 : [ RA_return_to_f3 ]
SP1+4 : [ n = 3 ]
SP1+0 : [ RA_return_to_caller ]
...

Each recursive call allocates its own frame (addi sp, sp, -8) and saves the current ra and a0 into that frame.
jal overwrites ra, so saving ra is essential before making another jal.
On return the function restores n and ra, frees its frame (addi sp, sp, 8), computes (mul) and then jr ra to jump back.
The stack grows downward; frames are popped in reverse order (LIFO), matching recursion order.

You might also like