Computer Organization & Computer Organization & Assembly Languages Assembly Languages
Computer Organization & Computer Organization & Assembly Languages Assembly Languages
Computer Organization & Computer Organization & Assembly Languages Assembly Languages
Assembly Languages
Introduction
Pu-Jen Cheng
2008/09/15
Course Administration
Instructor: Pu-Jen Cheng (CSIE R323)
pjcheng@csie.ntu.edu.tw
http://www.csie.ntu.edu.tw/~pjcheng
Class Hours: 2:00pm-5:00pm, Monday
Classroom: CSIE R102
TA(s): 戴瑋彥 b93705014@ntu.edu.tw
Course Information:
Announce: http://www.csie.ntu.edu.tw/~pjcheng/course/asm2008/
Q&A: bbs://ptt.cc → CSIE_ASM
Textbook
Basic Concepts of
Computer Computer Design
Organization
Computer Organization
This Course Computer Architecture
Assembly System
Language Software
http://en.wikipedia.org/wiki/Donald_Knuth
Course Coverage
Basic Concepts
IA-32 Processor Architecture
Assembly Language Fundamentals
Data Transfers, Addressing, and Arithmetic
Procedures
Conditional Processing
Integer Arithmetic
Advanced Procedures
Strings and Arrays
Structures and Macros
High-Level Language Interface
Assembler, Linker, and Loader
Other Advanced Topics (optional)
What You Will Learn
Basic principles of computer architecture
IA-32 processors and memory management
Basic assembly programming skills
How high-level language is translated to assembly
How assembly is translated to machine code
How application program communicates with OS
Interface between assembly to high-level language
Performance: Multiword Arithmetic
Longhand multiplication 0101
1101
¾ Final 128-bit result in P:A 0101
0101
P := 0; count := 64 0101
A := multiplier; B := multiplicand
while (count > 0)
if (LSB of A = 1)
then P := P+B
CF := carry generated by P+B
else CF := 0
end if
shift right CF:P:A by one bit position
count := count-1
end while
Example
A = 11012 (13)
B = 01012 (5)
After P+B After the shift
CF P A CF P A
Initial state ? 0000 1101 -- ---- ----
Iteration 1 0 0101 1101 ? 0010 1110
Iteration 2 0 0010 1110 ? 0001 0111
Iteration 3 0 0110 0111 ? 0011 0011
Iteration 4 0 1000 0011 ? 0100 0001
Time Comparison
5
4
C version
Timee (seconds)
2
ASM version
1
0
0 20 40 60 80 100
Number of calls (in millions)
Operating System
Level 3
Instruction Set
Architecture Level 2
Microarchitecture Level 1
Level 4
Instruction mnemonics that have a one-
to-one correspondence to machine
language
Calls functions written at the operating
system level (Level 3)
Programs are translated into machine
language (Level 2)
Operating System
Level 3
Provides services to Level 4 programs
Translated and run at the instruction set
architecture level (Level 2)
Instruction Set Architecture
Level 2
Also known as conventional machine
language
Executed by Level 1 (microarchitecture)
program
Microarchitecture
Level 1
Interprets conventional machine
instructions (Level 2)
Executed by digital hardware (Level 0)
Digital Logic
Level 0
CPU, constructed from digital logic gates
System bus
Memory
Every binary
number is a
sum of powers
of 2
Translating Binary to Decimal
Weighted positional notation shows how to
calculate the decimal value of each binary bit:
dec = (Dn-1 × 2n-1) + (Dn-2 × 2n-2) + ... + (D1 × 21)
+ (D0 × 20)
D = binary digit
37 = 100101
Binary Addition
Starting with the LSB, add each pair of digits, include the
carry if present.
carry: 1
0 0 0 0 0 1 0 0 (4)
+ 0 0 0 0 0 1 1 1 (7)
0 0 0 0 1 0 1 1 (11)
bit position: 7 6 5 4 3 2 1 0
Integer Storage Sizes
byte 8
Standard sizes:
word 16
doubleword 32
quadword 64
21 / 16 = 1, rem 5
16 + 5 = 21
−1
C6 75
A2 47
24 2E
sign bit
1 1 1 1 0 1 1 0
Negative
0 0 0 0 1 0 1 0 Positive
00001100 00001100
– 00000011 11111101
00001001
Advantages for 2’s complement:
No two 0’s
Sign bit
Remove the need for separate circuits for add
and sub
Ranges of Signed Integers
The highest bit is reserved for the sign. This limits
the range:
Fractional Binary Numbers
2i
2i–1
4
••• 2
1
bi bi–11 • • • b2 b1 b0 .b–11 b–22 b–33 • • • b–j
1/2
1/4
•••
1/8
Representation 2–j
Observations
¾ Divide by 2 by shifting right
¾ Multiply by 2 by shifting left
Value Representation
1/3
/ 0.0101010101[01]…2
1/5 0.001100110011[0011]…2
1/10 0.0001100110011[0011]…2
Converting Real Numbers
Binary real to decimal real
4.5625 = 100.10012
True or False
If x > 0 then x + 1 > 0
If x < 0 then x * 2 < 0
If x > y then -x < -y
If x >= 0 then -x <= 0
If x < 0 then -x > 0
If x >= 0 then (( !x – 1 ) & x ) == x
If x < 0 && y > 0 then x * y < 0
If x < 0 then ((x ^ x >> 31) + 1) > 0
Character Storage
Character sets
¾ Standard ASCII (0 – 127)
¾ Extended ASCII (0 – 255)
¾ ANSI (0 – 255)
¾ Unicode (0 – 65,535)
65 535)
Null-terminated String
¾ Array of characters followed by a null byte
Using the ASCII table
¾ back inside cover of book
Machine Words
Machine Has “Word Size”
¾ Nominal size of integer-valued data
Including addresses
Compatibility 31
33
31
33
¾ Byte ordering not an issue 00 00
Boolean Operations
NOT
AND
OR
Operator Precedence
Truth Tables
Boolean Algebra
Based on symbolic logic, designed by George Boole
Boolean expressions created from:
¾ NOT, AND, OR
NOT
Inverts (reverses) a boolean value
Truth table for Boolean NOT operator:
Digital gate diagram for NOT:
NOT
AND
Truth table for Boolean AND operator:
AND
OR
Truth table for Boolean OR operator:
OR
Operator Precedence
NOT > AND > OR
Examples showing the order of operations:
Example: ¬X ∨ Y
Truth Tables (2 of 3)
Example: X ∧ ¬Y
S
Truth Tables (3 of 3)
X
mux Z
Y
Example: (Y ∧ S) ∨ (X ∧ ¬S)
Two-input multiplexer