Assemblers
Kevin Walsh
CS 3410, Spring 2010
Computer Science
Cornell University
See: P&H Appendix B.1-2
Examples
...
T: ADDI r4, r0, -1
BEQ r3, r0, B
ADDI r4, r4, 1
LW r3, 0(r3)
J T
NOP
B: ...
L:
...
JAL L
nop
nop
LW r5, 0(r31)
ADDI r5, r5, 1
SW r5, 0(r31)
...
2
cs3410 Recap
C
compiler
MIPS
assembly
assembler
machine
code
int x = 10;
x = 2 * x + 15;
addi r5, r0, 10
muli r5, r5, 2
addi r5, r5, 15
00100000000001010000000000001010
00000000000001010010100001000000
00100000101001010000000000001111
CPU
Circuits
Gates
Transistors
Silicon
3
Example 1
...
T:ADDI r4,r0,-1
BEQ r3, r0, B
ADDI r4,r4, 1
LW r3, 0(r3)
J T
NOP
B:...
...
001000
000100
001000
100011
000010
00000000000000000000000000000000
...
4
References
Q: How to resolve labels into offsets and
addresses?
A: Two-pass assembly
• 1st pass: lay out instructions and data, and build
a symbol table (mapping labels to addresses) as you go
• 2nd pass: encode instructions and data in binary, using
symbol table to resolve references
5
Example 2
...
JAL L
nop
nop
L: LW r5, 0(r31)
ADDI r5,r5,1
SW r5, 0(r31)
...
...
00100000000100000000000000000100
00000000000000000000000000000000
00000000000000000000000000000000
10001111111001010000000000000000
00001000101001010000000000000001
00000000000000000000000000000000
...
6
Example 2 (better)
.text 0x00400000 # code segment
...
ORI r4, r0, counter
LW r5, 0(r4)
ADDI r5, r5, 1
SW r5, 0(r4)
...
.data 0x10000000 # data segment
counter:
.word 0
7
Lessons
Lessons:
•
•
•
•
Mixed data and instructions (von Neumann)
… but best kept in separate segments
Specify layout and data using assembler directives
Use pseudo-instructions
8
Pseudo-Instructions
Pseudo-Instructions
NOP # do nothing
MOVE reg, reg # copy between regs
LI reg, imm # load immediate (up to 32 bits)
LA reg, label # load address (32 bits)
B label # unconditional branch
BLT reg, reg, label # branch less than
9
Assembler
Assembler:
assembly instructions
+ psuedo-instructions
+ data and layout directives
= executable program
Slightly higher level than plain assembly
e.g: takes care of delay slots
(will reorder instructions or insert nops)
10
Motivation
Q: Will I program in assembly?
A: I do...
•
•
•
•
•
For kernel hacking, device drivers, GPU, etc.
For performance (but compilers are getting better)
For highly time critical sections
For hardware without high level languages
For new & advanced instructions: rdtsc, debug
registers, performance counters, synchronization, ...
11
Stages
calc.c
calc.s
calc.o
math.c
math.s
math.o
io.s
io.o
calc.exe
libc.o
libm.o
12
Anatomy of an executing program
0xfffffffc
top
0x80000000
0x7ffffffc
0x10000000
0x00400000
0x00000000
bottom
13
calc.c
Example program
vector v = malloc(8);
v->x = prompt(“enter x”);
v->y = prompt(“enter y”);
int c = pi + tnorm(v);
print(“result”, c);
math.c
int tnorm(vector v) {
return abs(v->x)+abs(v->y);
}
lib3410.o
global variable: pi
entry point: prompt
entry point: print
entry point: malloc
14
math.c
int abs(x) {
return x < 0 ? –x : x;
}
int tnorm(vector v) {
return abs(v->x)+abs(v->y);
}
math.s
tnorm:
# arg in r4, return address in r31
# leaves result in r4
abs:
# arg in r3, return address in r31
# leaves result in r3
15
dostuff:
calc.c
# no args, no return value, return addr in r31
vector v = malloc(8);
MOVE r30, r31
v->x = prompt(“enter x”);
LI r3, 8
# call malloc: arg in r3, ret in r3
v->y = prompt(“enter y”);
JAL malloc
int c = pi + tnorm(v);
MOVE r6, r3 # r6 holds v
print(“result”, c);
LA r3, str1 # call prompt: arg in r3, ret in r3
JAL prompt
.data
SW r3, 0(r6)
str1: .asciiz “enter x”
LA r3, str2 # call prompt: arg in r3, ret in r3
str2: .asciiz “enter y”
JAL prompt
str3: .asciiz “result”
SW r3, 4(r6)
.text
MOVE r4, r6 # call tnorm: arg in r4, ret in r4
.extern prompt
JAL tnorm
.extern print
LA r5, pi
.extern malloc
LW r5, 0(r5)
.extern tnorm
ADD r5, r4, r5
.global dostuff
LA r3, str3 # call print: args in r3 and r4
MOVE r4, r5
JAL print
JR r30
calc.s
16
© Copyright 2026 Paperzz