Rutgers University
Santosh Nagarakatte
10
Assembly Programming
Brief tour through assembly language programming
Why?
n
Machine interface: where software meets hardware
n
To understand how the hardware works, we have to
understand the interface that it exports
Why not binary language?
n
Much easier for humans to read and reason about
n
Major differences:
l
Human readable language instead of binary sequences
l
Relative instead of absolute addresses

Assembly Programmer’s View
Control
Logic
(OS code & data)
Object Code
Program Data
Addresses
Data
Instructions
Registers
ALU
CPU
Memory
Condition
Codes
PC

Memory
00101100
10001000
11111111
01010101
00000000
11000001
00000000
11111001
11111000
00110000
00000000
00000000
00000000
11000011
00011001
00000000
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
Memory
Addresses
Storage
Address
Command: R/W
Data
CPU

Memory Access: Read
00101100
10001000
11111111
01010101
00000000
11000001
00000000
11111001
11111000
00110000
00000000
00000000
00000000
11000011
00011001
00000000
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
Memory
Addresses
Storage
03
R
CPU
01010101

Memory Access: Write
00101100
10001000
11111111
01010101
00000000
11000001
00000000
11111001
11111000
00110000
00000000
00000000
00000000
11000011
00011001
00000000
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
Memory
Addresses
Storage
04
W
CPU
01010101

Memory Access: Write
00101100
10001000
11111111
01010101
01010101
11000001
00000000
11111001
11111000
00110000
00000000
00000000
00000000
11000011
00011001
00000000
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
Memory
Addresses
Storage
04
W
CPU
01010101

Processor: ALU & Registers
ALU
B
C
A
S
C = F
S
(A, B)
F includes
Arithmetic: +, -, *, /, ~, etc.
Logical: <, >, =, etc.
00101100
10001000
11111111
01010101
R0
R1
R2
R3
Name Storage
Registers
Name
Command: R/W
Data
Multiple Ports

Putting It All Together
ALU
Control
Logic
Registers
Program Counter (PC)
00101100
10001000
11111111
01010101
01010101
11000001
00000000
11111001
11111000
00110000
00000000
00000000
00000000
11000011
00011001
00000000
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
Memory
Addresses
Storage
CPU
Condition Codes

Putting It All Together
ALU
Control
Logic
Registers
Program Counter (PC)
00101100
10001000
11111111
01010101
01010101
11000001
00000000
11111001
11111000
00110000
00000000
00000000
00000000
11000011
00011001
00000000
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
Memory
Addresses
Storage
CPU
Condition Codes
1

Putting It All Together
ALU
Control
Logic
Registers
Program Counter (PC)
00101100
10001000
11111111
01010101
01010101
11000001
00000000
11111001
11111000
00110000
00000000
00000000
00000000
11000011
00011001
00000000
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
Memory
Addresses
Storage
CPU
Condition Codes
1
1
10001000

Putting It All Together
ALU
Control
Logic
Registers
Program Counter (PC)
00101100
10001000
11111111
01010101
01010101
11000001
00000000
11111001
11111000
00110000
00000000
00000000
00000000
11000011
00011001
00000000
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
Memory
Addresses
Storage
CPU
Condition Codes
1
1
10001000
+
R0
R1
R0: x
R1: y

Putting It All Together
ALU
Control
Logic
Registers
Program Counter (PC)
00101100
10001000
11111111
01010101
01010101
11000001
00000000
11111001
11111000
00110000
00000000
00000000
00000000
11000011
00011001
00000000
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
Memory
Addresses
Storage
CPU
Condition Codes
1
1
10001000
+
R0
R1
x
y
R0: x
R1: y

Putting It All Together
ALU
Control
Logic
Registers
Program Counter (PC)
00101100
10001000
11111111
01010101
01010101
11000001
00000000
11111001
11111000
00110000
00000000
00000000
00000000
11000011
00011001
00000000
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
Memory
Addresses
Storage
CPU
Condition Codes
2
1
10001000
+
R0
R1
x
y
z
R2: z
R0: x
R1: y
R2

Rutgers University
Santosh Nagarakatte
23
C & Assembly Code
int accum;
int sum(int x, int y)
{
int t = x + y;
accum += t;
return t;
}
gcc
-O1 -m32 –S code.c
Sample C Code
sum:
push %ebp
movl %esp, %ebp
movl 12(%ebp), %eax
addl 8(%ebp), %eax
addl %eax, accum
popl %ebp
ret
Generated Assembly

Rutgers University
Santosh Nagarakatte
24
C & Machine Code
int accum;
int sum(int x, int y){
int t = x + y;
accum += t;
return t;
}
gcc
-O1 -m32 –c code.c
Sample C Code
(gdb) x/100xb sum
gdb code.o
<sum>: 0x55 0x89 0xe5 0x8b 0x45 0x0c 0x03 0x45
0x8 <sum+8>: 0x08 0x01 0x05 0x00 0x00
0x00
0x00
0x5d


You've reached the end of your free preview.
Want to read all 155 pages?
- Fall '15
- SantoshNagarakatte
- Computer Architecture, Processor register, Call stack, Addressing mode, eax, Prof. Santosh Nagarakatte, Santosh Nagarakatte