0% found this document useful (0 votes)
18 views40 pages

EE209A - 24 14 Assembly1

Uploaded by

김문엽
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)
18 views40 pages

EE209A - 24 14 Assembly1

Uploaded by

김문엽
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
You are on page 1/ 40

EE209: Programming Structures for

Electrical Engineering
Lecture 12: Assembly Language:
Overview
Reminder: how an executable is built

text C program (p1.c p2.c)

Compiler
text Asm program (p1.s p2.s)

Assembler
binary Object program (p1.o p2.o)

Linker
binary Static libraries
Executable program (p) (.a)

3
High-Level Languages

• Make programming easier by describing


operations in a semi-natural language
count = 0;
• Increase the portability of the code while (n > 1) {
count++;
• One line may involve many low-level operations if (n & 1)
• Examples: C, C++, Java, Pascal, …
n = n*3 + 1;
else
n = n/2;
}

4
Assembly Language

• Tied to the specifics of the movl $0, %ecx


loop:
underlying machine cmpl $1, %edx
jle endloop
• Commands and names to make
addl $1, %ecx
the code readable and writeable
movl %edx, %eax
by humans andl $1, %eax
je else
• Hand-coded assembly code may
movl %edx, %eax
be more efficient addl %eax, %edx
addl %eax, %edx
• e.g., IA-32 from Intel addl $1, %edx
jmp endif
else:
sarl $1, %edx
endif:
jmp loop
endloop:

5
Machine Language

• Also tied to the underlying machine


0000 0000 0000 0000 0000 0000 0000 0000

• What the computer sees and deals 0000 0000 0000 0000 0000 0000 0000 0000
9222 9120 1121 A120 1121 A121 7211 0000
with 0000 0001 0002 0003 0004 0005 0006 0007
0008 0009 000A 000B 000C 000D 000E 000F
• Every instruction is a sequence of 0000 0000 0000 FE10 FACE CAFE ACED CEDE

one or more numbers

• All stored in memory on the 1234 5678 9ABC DEF0 0000 0000 F00D 0000
0000 0000 EEEE 1111 EEEE 1111 0000 0000
computer, and read and executed B1B2 F1F5 0000 0000 0000 0000 0000 0000

• Unreadable by humans

6
Why Learn Assembly Language?

• Understand how things work underneath


• Learn the basic organization of the underlying machine
• Learn how the computer actually runs a program
• Design better computers in the future

• Write faster code (even in high-level language)


• By understanding which high-level constructs are better
• … in terms of how efficient they are at the machine level

• Some software is still written in assembly language


• Code that really needs to run quickly
• Code for embedded systems, network processors, etc.

7
Computer Architecture

•1
0 8
A Typical Computer

CPU ... CPU

Memor Chips
y et
I/O bus
RO
M

Network

9
Von Neumann Architecture

• Central Processing Unit (CPU)


CPU
• Control unit Control
• Fetch, decode, and execute Unit
• Arithmetic and logic unit (ALU) ALU
• Execution of low-level operations
• General-purpose registers
Registers
• High-speed temporary storage
• Data bus
• Provide access to memory
Data bus

Random Access
Memory (RAM)

10
Von Neumann Architecture
• Process’ Address Space
• Store executable machine-language instructions CPU
(text section) Control
• Store data (rodata, data, bss (block starting symbol),
Unit
ALU
heap, and stack sections)

TEXT
Registers
RODATA
DATA
BSS
HEAP Data bus

Random Access
STACK Memory (RAM)

11
Control Unit: Instruction Pointer
• Stores the location of the next instruction.

• Changing the instruction pointer (EIP)


• Increment to go to the next instruction
• Or, load a new value to “jump” to a new location
• “E” is for extended; from 16 bits extended to 32 bits

EIP

12
Control Unit: Instruction Decoder
• Determines what operations need to take place
• Translate the machine-language instruction

• Control what operations are done on what data


• e.g., control what data are fed to the ALU
• e.g., enable the ALU to do multiplication or addition
• e.g., read from a particular address in memory

src1 src2

operation
ALU ALU flag/carry

dst
13
Registers
• Small amount of storage on the CPU
• Can be accessed more quickly than main memory

• Instructions that move data in and out of registers


• Loading registers from main memory
• Storing registers to main memory

• Instructions that manipulate the register contents


• Registers essentially act as temporary variables
• For efficient manipulation of the data

• Registers are the top of the memory hierarchy


• Ahead of main memory, disk, tape, …

14
Keeping it Simple: All 32-bit Words
• Simplifying assumption: all data in four-byte units
• Memory is 32 bits wide
• Registers are 32 bits wide
32bit

32bit
EAX
EBX

• In practice, can manipulate different sizes of data

https://docs.microsoft.com/en-us/windows-
hardware/drivers/debugger/processor-architecture
15
C Code vs. Assembly Code

•1
8 16
Kinds of Instructions

• Reading and writing data


• count = 0
count = 0; • n
while (n > 1) { • Arithmetic and logic operations
count++; • Increment: count++
if (n & 1) • Multiply: n * 3
n = n*3 + 1; • Divide: n / 2
else • Bitwise AND: n & 1
n = n/2; • Checking results of comparisons
} • Is (n > 1) true or false?
• Is (n & 1) non-zero or zero?
• Changing the flow of control
• To the end of the while loop (if “n > 1”)
• Back to the beginning of the loop
• To the else clause (if “n & 1” is 0)

17
Variables in Registers

count = 0;
while (n > 1) {
count++; Registers
if (n & 1)
n = n*3 + 1; n %edx
else count %ecx
n = n/2;
}

Referring to a register: percent sign (“%”)

Useful link:
https://docs.microsoft.com/en-us/windows-
hardware/drivers/debugger/x86-architecture
18
Immediate and Register Addressing
Operand size is
32 bits

count=0; movl $0, %ecx


while (n>1) {
count++; addl $1, %ecx
if (n&1)
n = n*3+1;
else Read directly written to
n = n/2; from the a register
instruction
}
2-operator instruction format:
operator Src, Dest 🡪 Dest = Dest + Src

Referring to an immediate operand (or a constant): dollar sign (“$”)

19
Immediate and Register Addressing

count=0;
while (n>1) { n

count++;
movl %edx, %eax
if (n&1) andl $1, %eax
n = n*3+1;
else
n = n/2;
}

Computing the intermediate value in register EAX

20
Immediate and Register Addressing

count=0;
while (n>1) {
count++;
if (n&1)
movl %edx, %eax
n = n*3+1; addl %eax, %edx
else addl %eax, %edx
addl $1, %edx
n = n/2;
}

Adding n twice is cheaper than multiplication!


note: not all multiplications are translated into a series of
additions
21
Immediate and Register Addressing

count=0;
while (n>1) {
count++;
if (n&1)
n = n*3+1;
else
n = n/2; sarl $1, %edx
}

Shifting right by 1 bit is cheaper than division!

22
Changing Program Flow
• Cannot simply run next instruction
• Check result of a previous operation
• Jump to appropriate next instruction

count=0; • Flags register (EFLAGS)


while (n>1) { • Stores the status of the operations, such as

count++; comparisons, as a side effect


• e.g., last result was positive, negative, zero,
if (n&1)
had carry, etc.
n = n*3+1;
• Jump instructions
else
• Load new address in instruction pointer
n = n/2;
• Example jump instructions
}
• Jump unconditionally (e.g., “}”)
• Jump if zero (e.g., “n&1”)
• Jump if greater/less (e.g., “n>1”)

23
Conditional and Unconditional Jumps

• cmpl: compares two integers


• Done by subtracting the first number from the second
• Discarding the results, but setting flags as a side effect
• Example:
• cmpl $1, %edx (computes %edx – 1)
• jle endloop (checks whether result was 0 or negative)

• andl : performs logical AND


• Example:
• andl $1, %eax (bit-wise AND of %eax with 1)
• je else (checks whether result was 0)

• Also, can do an unconditional branch jmp


• Example:
• jmp endif and jmp loop

24
Jump and Labels: While Loop

loop:
cmpl $1, %edx
jle endloop
while (n>1) { …

jmp loop
endloop:
25
Jump and Labels: While Loop

loop:
cmpl $1, %edx
jle endloop
while (n>1) { …

Checking if EDX
is less than or
equal to 1.

jmp loop
endloop:
26
Jump and Labels: If-Then-Else

movl $0, %ecx


loop:
cmpl $1, %edx
count=0; jle endloop
while (n>1) { addl $1, %ecx
count++; movl %edx, %eax
andl $1, %eax
if (n&1) je else
n = n*3+1; movl %edx, %eax
addl %eax, %edx
else addl %eax, %edx
n = n/2; addl $1, %edx
jmp endif
} else:
sarl $1, %edx
endif:
jmp loop
endloop:
27
Jump and Labels: If-Then-Else

movl %edx, %eax


andl $1, %eax
je else
if (n&1) …
...
else “then” block
... jmp endif
else:

endif:

28
Jump and Labels: If-Then-Else

movl %edx, %eax


andl $1, %eax
if (n&1) je else
... …
else
...
jmp endif
else:
“else” block …
endif:

29
Jump and Labels: If-Then-Else
movl $0, %ecx
loop:
cmpl $1, %edx
count=0; jle endloop
while(n>1) { addl $1, %ecx
count++; movl %edx, %eax
andl $1, %eax
if (n&1) je else
n = n*3+1; movl %edx, %eax
addl %eax, %edx
else addl %eax, %edx
“then” block addl $1, %edx
n = n/2;
} jmp endif
else:
“else” block sarl $1, %edx
endif:
jmp loop
endloop:

30
Making the Code More Efficient…

movl $0, %ecx


loop:
cmpl $1, %edx
count=0; jle endloop
while(n>1) { addl $1, %ecx
count++; movl %edx, %eax
andl $1, %eax
if (n&1) je else
n = n*3+1; movl %edx, %eax
addl %eax, %edx
else addl %eax, %edx
n = n/2; addl $1, %edx
} jmp endif
else:
sarl $1, %edx
endif:
jmp loop
endloop:

31
Making the Code More Efficient…

movl $0, %ecx


loop:
cmpl $1, %edx
count=0; jle endloop
while(n>1) { addl $1, %ecx
count++; movl %edx, %eax
andl $1, %eax
if (n&1) je else
n = n*3+1; movl %edx, %eax
addl %eax, %edx
else addl %eax, %edx
n = n/2; addl $1, %edx
} jmp endif
else:
sarl $1, %edx
endif:
Replace with jmp loop
“jmp loop” endloop:

32
Complete Example

loop:
cmpl $1, %edx
count=0; jle endloop
while (n>1) {
count++; movl %edx, %eax
if (n&1) andl $1, %eax
je else
n = n*3+1;
else
n = n/2;
}
jmp endif
else:
sarl $1, %edx
endif:
jmp loop
endloop:
33
Complete Example
n %edx
count %ecx

loop:
cmpl $1, %edx
count=0; jle endloop
while (n>1) {
count++; movl %edx, %eax
if (n&1) andl $1, %eax
je else
n = n*3+1;
else
n = n/2;
}
jmp endif
else:
sarl $1, %edx
endif:
jmp loop
endloop:
34
Complete Example
n %edx
count %ecx
movl $0, %ecx
loop:
cmpl $1, %edx
count=0; jle endloop
while (n>1) {
count++; movl %edx, %eax
if (n&1) andl $1, %eax
je else
n = n*3+1;
else
n = n/2;
}
jmp endif
else:
sarl $1, %edx
endif:
jmp loop
endloop:
35
Complete Example
n %edx
count %ecx
movl $0, %ecx
loop:
cmpl $1, %edx
count=0; jle endloop
while (n>1) { addl $1, %ecx
count++; movl %edx, %eax
if (n&1) andl $1, %eax
je else
n = n*3+1;
else
n = n/2;
}
jmp endif
else:
sarl $1, %edx
endif:
jmp loop
endloop:
36
Complete Example
n %edx
count %ecx
movl $0, %ecx
loop:
cmpl $1, %edx
count=0; jle endloop
while (n>1) { addl $1, %ecx
count++; movl %edx, %eax
if (n&1) andl $1, %eax
je else
n = n*3+1; movl %edx, %eax
else addl %eax, %edx
n = n/2; addl %eax, %edx
} addl $1, %edx
jmp endif
else:
sarl $1, %edx
endif:
jmp loop
endloop:
37
Reading IA-32 Assembly Language

• Referring to a register: percent sign (“%”)


• e.g., “%ecx” or “%eip”

• Referring to immediate operand: dollar sign (“$”)


• e.g., “$1” for the number 1

• Storing result: typically in the second argument


• e.g. “addl $1, %ecx” increments register ECX
• e.g., “movl %edx, %eax” moves EDX to EAX

• Assembler directives: starting with a period (“.”)


• e.g., “.section .text” to start the text section of memory

• Comment: pound sign (“#”)


• e.g., “# Purpose: Convert lower to upper case”

38
Conclusions

• Assembly language
• In between high-level language and machine code

• Programming the “bare metal” of the hardware

• Loading and storing data, arithmetic and logic operations, checking


results, and changing control flow

• To get more familiar with IA-32 assembly


• Read more assembly-language examples
• Chapter 3 of Bryant and O’Hallaron book
• The book has 64-bit assembly language (x86-64) – the key idea is the same while it’s more complex

• Generate your own assembly-language code


• gcc209 –S –O2 code.c
• (32bit) gcc209 –S -O2 –m32 code.c

39
40

You might also like