SlideShare a Scribd company logo
Course overview
Computer Organization and Assembly Languages
p g z y g g
Yung-Yu Chuang
with slides by Kip Irvine
Logistics
• Meeting time: 2:20pm-5:20pm, Monday
Classroom: CSIE Room 103
• Classroom: CSIE Room 103
• Instructor: 莊永裕 Yung-Yu Chuang
T hi i t t 黃子桓/李根逸/蘇彥禎/張哲瀚
• Teaching assistants:黃子桓/李根逸/蘇彥禎/張哲瀚
• Webpage:
http://www.csie.ntu.edu.tw/~cyy/asm
id / password
p
• Forum:
http://www.cmlab.csie.ntu.edu.tw/~cyy/forum/viewforum.php?f=21
• Mailing list: assembly@cmlab.csie.ntu.edu.tw
Please subscribe via
https://cmlmail.csie.ntu.edu.tw/mailman/listinfo/assembly/
Prerequisites
• Better to have programming experience with
some high level languages such C C ++ Java
some high-level languages such C, C ++,Java …
• Note: it is not tested in your graduate school
entrance exam, and not listed as a required
course anymore.
Textbook
• Readings and slides
References (TOY)
Princeton’s Introduction to CS,
htt // i t d /i t
http://www.cs.princeton.edu/intro
cs/50machine/
http://www.cs.princeton.edu/intro
cs/60circuits/
References (ARM)
ARM Assembly Language
P i P t K d
Programming, Peter Knaggs and
Stephen Welsh
ARM System Developer’s Guide,
Andrew Sloss, Dominic Symes and
Andrew Sloss, Dominic Symes and
Chris Wright
References (ARM)
Whirlwind Tour of ARM Assembly,
TONC J Vij
TONC, Jasper Vijn.
ARM System-on-chip Architecture
ARM System on chip Architecture,
Steve Furber.
References (IA32)
Assembly Language for Intel-Based
C t 5th Editi Ki I i
Computers, 5th Edition, Kip Irvine
Th A t f A bl L R d
The Art of Assembly Language, Randy
Hyde
References (IA32)
Michael Abrash' s Graphics Programming
Bl k B k
Black Book
C t S t A P '
Computer Systems: A Programmer's
Perspective, Randal E. Bryant and David
R O'H ll
R. O'Hallaron
Grading (subject to change)
• Assignments (4~5 projects, 52%), most graded
by performance
by performance
• Class participation (5%)
• Midterm exam (18%)
• Final project (25%)
p j ( )
– Examples from previous years
Computer Organization and Assembly language
• It is not only about assembly but also about
“computer organization”
computer organization .
Early computers
Early programming tools
First popular PCs
Early PCs
• Intel 8086
processor
processor
• 768KB memory
• 20MB disk
• Dot-Matrix
printer (9-pin)
GUI/IDE
More advanced architectures
• Pipeline
SIMD
• SIMD
• Multi-core
• Cache
More advanced software
More “computers” around us
My computers
Desktop
(Intel Pentium D
3GHz Nvidia 7900)
VAIO Z46TD
(I l C 2 D P9700 2 8GH )
3GHz, Nvidia 7900)
(Intel Core 2 Duo P9700 2.8GHz)
iPhone 3GS
(ARM Cortex-A8
GBA SP
833MHz)
GBA SP
(ARM7 16.78MHz)
Computer Organization and Assembly language
• It is not only about assembly but also about
“computer organization”
computer organization .
• It will cover
– Basic concept of computer systems and architecture
– ARM architecture and assembly language
– x86 architecture and assembly language
TOY machine
TOY machine
• Starting from a simple construct
TOY machine
• Build several components and connect them
together
together
TOY machine
• Almost as good as any computers
TOY machine
A DUP 32
int A[32]; 10: C020
lda R1, 1
lda RA, A
20: 7101
21: 7A00
lda RC, 0
d ld RD 0 FF
i=0;
Do {
RD tdi
22: 7C00
23 8DFF
read ld RD, 0xFF
bz RD, exit
add R2 RA RC
RD=stdin;
if (RD==0) break;
23: 8DFF
24: CD29
25: 12AC
add R2, RA, RC
sti RD, R2
add RC, RC, R1
A[i]=RD;
i=i+1;
25: 12AC
26: BD02
27: 1CC1
bz R0, read
it jl RF i t
} while (1);
i t ()
28: C023
29 FF2B
exit jl RF, printr
hlt
printr(); 29: FF2B
2A: 0000
ARM
• ARM architecture
ARM bl i
• ARM assembly programming
IA32
• IA-32 Processor Architecture
• Data Transfers Addressing and Arithmetic
• Data Transfers, Addressing, and Arithmetic
• Procedures
• Conditional Processing
g
• Integer Arithmetic
• Advanced Procedures
• Strings and Arrays
• High-Level Language Interface
• Real Arithmetic (FPU)
• SIMD
• Code Optimization
What you will learn
• Basic principle of computer architecture
H k
• How your computer works
• How your C programs work
• Assembly basics
• ARM assembly programming
• ARM assembly programming
• IA-32 assembly programming
S ifi t FPU/MMX
• Specific components, FPU/MMX
• Code optimization
• Interface between assembly to high-level
language
g g
Why taking this course?
• Does anyone really program in assembly
nowadays?
nowadays?
Yes at times you do need to write assembly
• Yes, at times, you do need to write assembly
code.
• It is foundation for computer architecture and
• It is foundation for computer architecture and
compilers. It is related to electronics, logic
design and operating system
design and operating system.
CSIE courses
• Hardware: electronics, digital system,
architecture
architecture
• Software: operating system, compiler
wikipedia
• Today, assembly language is used primarily for
direct hardware manipulation access to
direct hardware manipulation, access to
specialized processor instructions, or to address
critical performance issues Typical uses
critical performance issues. Typical uses
are device drivers, low-level embedded systems,
and real time systems
and real-time systems.
Reasons for not using assembly
• Development time: it takes much longer to
develop in assembly Harder to debug no type
develop in assembly. Harder to debug, no type
checking, side effects…
M i t i bilit t t d di t t i k
• Maintainability: unstructured, dirty tricks
• Portability: platform-dependent
Reasons for using assembly
• Educational reasons: to understand how CPUs
and compilers work Better understanding to
and compilers work. Better understanding to
efficiency issues of various constructs.
D l i il d b d th
• Developing compilers, debuggers and other
development tools.
• Hardware drivers and system code
• Embedded systems
y
• Developing libraries.
• Accessing instructions that are not available
• Accessing instructions that are not available
through high-level languages.
O ti i i f d
• Optimizing for speed or space
To sum up
• It is all about lack of smart compilers
• Faster code, compiler is not good enough
• Smaller code , compiler is not good enough, e.g.
mobile devices, embedded devices, also
, ,
Smaller code → better cache performance →
faster code
• Unusual architecture , there isn’t even a
compiler or compiler quality is bad eg GPU
compiler or compiler quality is bad, eg GPU,
DSP chips, even MMX.
Overview
• Virtual Machine Concept
p
• Data Representation
• Boolean Operations
• Boolean Operations
Translating languages
English: Display the sum of A times B plus C
English: Display the sum of A times B plus C.
C++:
cout << (A * B + C);
cout << (A B + C);
Intel Machine Language:
Assembly Language:
mov eax,A
Intel Machine Language:
A1 00000000
F7 25 00000004
mul B
add eax,C
ll W it I t
F7 25 00000004
03 05 00000008
E8 00500000
call WriteInt E8 00500000
Virtual machines
Abstractions for computers
High-Level Language Level 5
Assembly Language Level 4
Operating System
Instruction Set
Level 3
Architecture
Microarchitecture Level 1
Level 2
Digital Logic Level 0
High-level language
• Level 5
• Application-oriented languages
• Programs compile into assembly language
Programs compile into assembly language
(Level 4)
cout << (A * B + C);
Assembly language
• Level 4
• Instruction mnemonics that have a one-to-one
correspondence to machine language
• Calls functions written at the operating
system level (Level 3)
y ( )
• Programs are translated into machine
language (Level 2)
language (Level 2)
mov eax, A
mul B
mul B
add eax, C
call WriteInt
Operating system
• Level 3
• Provides services
• Programs translated and run at the instruction
g
set architecture level (Level 2)
Instruction set architecture
• Level 2
• Also known as conventional machine language
• Executed by Level 1 program
y p g
(microarchitecture, Level 1)
A1 00000000
F7 25 00000004
03 05 00000008
E8 00500000
Microarchitecture
• Level 1
• Interprets conventional machine instructions
(Level 2)
• Executed by digital hardware (Level 0)
Digital logic
• Level 0
CPU d f di i l l i
• CPU, constructed from digital logic gates
• System bus
• Memory
Data representation
• Computer is a construction of digital circuits
with two states: on and off
with two states: on and off
• You need to have the ability to translate
b t diff t t ti t i
between different representations to examine
the content of the machine
• Common number systems: binary, octal,
decimal and hexadecimal
Binary representations
• Electronic Implementation
E t t ith bi t bl l t
– Easy to store with bistable elements
– Reliably transmitted on noisy and inaccurate wires
0 1 0
2.8V
3.3V
0.0V
0.5V
Binary numbers
• Digits are 1 and 0
( bi di it i ll d bit)
(a binary digit is called a bit)
1 = true
0 = false
• MSB –most significant bit
• LSB –least significant bit
MSB LSB
• Bit numbering: 1 0 1 1 0 0 1 0 1 0 0 1 1 1 0 0
MSB LSB
A bit string could have different interpretations
0
15
• A bit string could have different interpretations
Unsigned binary integers
• Each digit (bit) is either 1 or 0
• Each bit represents a power of 2: 1 1 1 1 1 1 1 1
27 26 25 24 23 22 21 20
Every binary
number is a
f
sum of powers
of 2
Translating binary to decimal
Weighted positional notation shows how to
Weighted positional notation shows how to
calculate the decimal value of each binary bit:
d (D 2n 1) (D 2n 2) (D 21) (D
dec = (Dn-1  2n-1)  (Dn-2  2n-2)  ...  (D1  21)  (D0
 20)
D = binary digit
binary 00001001 = decimal 9:
(1 23) (1 20) 9
(1  23) + (1  20) = 9
Translating unsigned decimal to binary
• Repeatedly divide the decimal integer by 2. Each
remainder is a binary digit in the translated value:
remainder is a binary digit in the translated value:
37 = 100101
37 = 100101
Binary addition
• Starting with the LSB, add each pair of digits,
include the carry if present
include the carry if present.
1
carry:
0 0 0 0 0 1 0 0
1
(4)
carry:
0 0 0 0 0 1 1 1
+ (7)
0 0 0 0 1 0 1 1
0 0 0 0 1 0 1 1 (11)
0
1
2
3
4
bit position: 5
6
7 0
1
2
3
4
bit position: 5
6
7
Integer storage sizes
byte
16
8
Standard sizes: 16
32
word
doubleword
64
quadword
Standard sizes:
64
quadword
Practice: What is the largest unsigned integer that may be stored in 20 bits?
Practice: What is the largest unsigned integer that may be stored in 20 bits?
Large measurements
• Kilobyte (KB), 210 bytes
M b (MB) 220 b
• Megabyte (MB), 220 bytes
• Gigabyte (GB), 230 bytes
• Terabyte (TB), 240 bytes
• Petabyte
• Petabyte
• Exabyte
Z tt b t
• Zettabyte
• Yottabyte
Hexadecimal integers
All values in memory are stored in binary. Because long
binary numbers are hard to read we use hexadecimal
binary numbers are hard to read, we use hexadecimal
representation.
Translating binary to hexadecimal
• Each hexadecimal digit corresponds to 4 binary
bits.
• Example: Translate the binary integer
• Example: Translate the binary integer
000101101010011110010100 to hexadecimal:
Converting hexadecimal to decimal
• Multiply each digit by its corresponding
f 16
power of 16:
dec = (D3  163) + (D2  162) + (D1  161) + (D0  160)
H 1234 l (1 163) + (2 162) + (3 161) + (4
• Hex 1234 equals (1  163) + (2  162) + (3  161) + (4
 160), or decimal 4,660.
• Hex 3BA4 equals (3  163) + (11 * 162) + (10  161)
Hex 3BA4 equals (3  16 ) + (11 16 ) + (10  16 )
+ (4  160), or decimal 15,268.
Powers of 16
Used when calculating hexadecimal values up to
8 digits long:
Converting decimal to hexadecimal
decimal 422 = 1A6 hexadecimal
Hexadecimal addition
Divide the sum of two digits by the number base
(16) Th ti t b th l d
(16). The quotient becomes the carry value, and
the remainder is the sum digit.
36 28 28 6A
1
1
36 28 28 6A
42 45 58 4B
78 6D 80 B5
78 6D 80 B5
Important skill: Programmers frequently add and subtract the
addresses of variables and instructions
addresses of variables and instructions.
Hexadecimal subtraction
When a borrow is required from the digit to the
l ft dd 10h t th t di it' l
left, add 10h to the current digit's value:
C6 75
1
A2 47
24 2E
Practice: The address of var1 is 00400020. The address of the next
variable after var1 is 0040006A How many bytes are used by var1?
variable after var1 is 0040006A. How many bytes are used by var1?
Signed integers
The highest bit indicates the sign. 1 = negative,
0 i i
0 = positive
sign bit
sign bit
1 1 1 1 0 1 1 0
Negative
0 0 0 0 1 0 1 0 Positive
If the highest digit of a hexadecmal integer is > 7, the value is
negative Examples: 8A C5 A2 9D
negative. Examples: 8A, C5, A2, 9D
Two's complement notation
Steps:
Complement (reverse) each bit
– Complement (reverse) each bit
– Add 1
Note that 00000001 + 11111111 = 00000000
Binary subtraction
• When subtracting A – B, convert B to its two's
complement
complement
• Add A to (–B)
0 1 0 1 0 0 1 0 1 0
– 0 1 0 1 1 1 0 1 0 0
1 1 1 1 1
Advantages for 2’s complement:
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:
the range:
Character
• Character sets
St d d ASCII(0 127)
– Standard ASCII(0 – 127)
– Extended ASCII (0 – 255)
ANSI (0 255)
– ANSI (0 – 255)
– Unicode (0 – 65,535)
• Null-terminated String
– Array of characters followed by a null byte
• Using the ASCII table
– back inside cover of book
Representing Instructions
int sum(int x, int y)
{ PC sum
Alpha sum Sun sum
{
return x+y;
}
55
89
00
00
p
81
C3
– For this example, Alpha &
Sun use two 4-byte
E5
8B
45
30
42
01
E0
08
90
instructions
• Use differing numbers of
instructions in other cases
0C
03
45
80
FA
6B
02
00
09
instructions in other cases
– PC uses 7 instructions
with lengths 1, 2, and 3
08
89
EC
Diff t hi t t ll diff t
g , ,
bytes
• Same for NT and for Linux
EC
5D
C3
Different machines use totally different
instructions and encodings
• NT / Linux not fully binary
compatible
Boolean algebra
• Boolean expressions created from:
– NOT, AND, OR
NOT
• Inverts (reverses) a boolean value
• Truth table for Boolean NOT operator:
Digital gate diagram for NOT:
N
O
T
N
O
T
AND
• Truth if both are true
• Truth table for Boolean AND operator:
Digital gate diagram for AND:
AND
OR
• True if either is true
• Truth table for Boolean OR operator:
Digital gate diagram for OR:
O R
Operator precedence
• NOT > AND > OR
• Examples showing the order of operations:
• Use parentheses to avoid ambiguity
Use parentheses to avoid ambiguity
Implementation of gates
• Fluid switch (http://www.cs.princeton.edu/introcs/lectures/fluid-computer.swf)
Implementation of gates
Implementation of gates
Truth Tables (1 of 2)
• A Boolean function has one or more Boolean
i d i l B l
inputs, and returns a single Boolean output.
• A truth table shows all the inputs and outputs
of a Boolean function
Example: X  Y
Truth Tables (2 of 2)
• Example: X  Y

More Related Content

PDF
Introduction to computers
PPTX
C_Programming_Notes_ICE
PPTX
Computer Programming In C.pptx
PDF
Introduction to Processor Design in System Verilog
PPTX
C Programming Lab_Program_Presentation__a
PDF
Programing fundamentals with C++
PPTX
Embedded programming Embedded programming (1).pptx
PPTX
Unit ii
Introduction to computers
C_Programming_Notes_ICE
Computer Programming In C.pptx
Introduction to Processor Design in System Verilog
C Programming Lab_Program_Presentation__a
Programing fundamentals with C++
Embedded programming Embedded programming (1).pptx
Unit ii

Similar to lec01_introanalytics-for-the-internet-of-things-iot-intelligent-analytics-for-your-intelligent-devices_compress (20)

PPT
9781285852744 ppt ch01
PPT
9781285852751 ppt c++
PPTX
Chapter 1 Introduction to computer components
PDF
Introduction to C programming
PPTX
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
PPTX
PPTX
PPTX
C++ programming languages lectures
PDF
Cse115 lecture02overviewofprogramming
PPTX
Compilers.pptx
PPTX
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
PPTX
Unit 1.pptx
PPTX
Master the Fundamentals of C Programming Language
PPTX
Programming 8051 with C and using Keil uVision5.pptx
PPTX
CSE_1201_Lecture_1_Introduction_to_Programming_0fd134f8149173dfa0821f1575f733...
PPTX
Lecture1.Introduction to Computer programming.pptx
PPTX
Embedded c c++ programming fundamentals master
PPT
Assembly Langauge Assembly Langauge Assembly Langauge
PPTX
FPL - Part 1 (Sem - I 2013 )
PPTX
Microcontroller 5th sem notes AIML branch 2.pptx
9781285852744 ppt ch01
9781285852751 ppt c++
Chapter 1 Introduction to computer components
Introduction to C programming
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C++ programming languages lectures
Cse115 lecture02overviewofprogramming
Compilers.pptx
UNIT - 1jhjhjbkjhkjhkjhkjhkjhhkkhhh.pptx
Unit 1.pptx
Master the Fundamentals of C Programming Language
Programming 8051 with C and using Keil uVision5.pptx
CSE_1201_Lecture_1_Introduction_to_Programming_0fd134f8149173dfa0821f1575f733...
Lecture1.Introduction to Computer programming.pptx
Embedded c c++ programming fundamentals master
Assembly Langauge Assembly Langauge Assembly Langauge
FPL - Part 1 (Sem - I 2013 )
Microcontroller 5th sem notes AIML branch 2.pptx
Ad

Recently uploaded (20)

PPTX
Sustainable Sites - Green Building Construction
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Well-logging-methods_new................
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
web development for engineering and engineering
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
DOCX
573137875-Attendance-Management-System-original
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPT
Project quality management in manufacturing
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Sustainable Sites - Green Building Construction
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
UNIT 4 Total Quality Management .pptx
Well-logging-methods_new................
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Foundation to blockchain - A guide to Blockchain Tech
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
web development for engineering and engineering
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
573137875-Attendance-Management-System-original
CYBER-CRIMES AND SECURITY A guide to understanding
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Project quality management in manufacturing
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Ad

lec01_introanalytics-for-the-internet-of-things-iot-intelligent-analytics-for-your-intelligent-devices_compress

  • 1. Course overview Computer Organization and Assembly Languages p g z y g g Yung-Yu Chuang with slides by Kip Irvine
  • 2. Logistics • Meeting time: 2:20pm-5:20pm, Monday Classroom: CSIE Room 103 • Classroom: CSIE Room 103 • Instructor: 莊永裕 Yung-Yu Chuang T hi i t t 黃子桓/李根逸/蘇彥禎/張哲瀚 • Teaching assistants:黃子桓/李根逸/蘇彥禎/張哲瀚 • Webpage: http://www.csie.ntu.edu.tw/~cyy/asm id / password p • Forum: http://www.cmlab.csie.ntu.edu.tw/~cyy/forum/viewforum.php?f=21 • Mailing list: assembly@cmlab.csie.ntu.edu.tw Please subscribe via https://cmlmail.csie.ntu.edu.tw/mailman/listinfo/assembly/
  • 3. Prerequisites • Better to have programming experience with some high level languages such C C ++ Java some high-level languages such C, C ++,Java … • Note: it is not tested in your graduate school entrance exam, and not listed as a required course anymore.
  • 5. References (TOY) Princeton’s Introduction to CS, htt // i t d /i t http://www.cs.princeton.edu/intro cs/50machine/ http://www.cs.princeton.edu/intro cs/60circuits/
  • 6. References (ARM) ARM Assembly Language P i P t K d Programming, Peter Knaggs and Stephen Welsh ARM System Developer’s Guide, Andrew Sloss, Dominic Symes and Andrew Sloss, Dominic Symes and Chris Wright
  • 7. References (ARM) Whirlwind Tour of ARM Assembly, TONC J Vij TONC, Jasper Vijn. ARM System-on-chip Architecture ARM System on chip Architecture, Steve Furber.
  • 8. References (IA32) Assembly Language for Intel-Based C t 5th Editi Ki I i Computers, 5th Edition, Kip Irvine Th A t f A bl L R d The Art of Assembly Language, Randy Hyde
  • 9. References (IA32) Michael Abrash' s Graphics Programming Bl k B k Black Book C t S t A P ' Computer Systems: A Programmer's Perspective, Randal E. Bryant and David R O'H ll R. O'Hallaron
  • 10. Grading (subject to change) • Assignments (4~5 projects, 52%), most graded by performance by performance • Class participation (5%) • Midterm exam (18%) • Final project (25%) p j ( ) – Examples from previous years
  • 11. Computer Organization and Assembly language • It is not only about assembly but also about “computer organization” computer organization .
  • 15. Early PCs • Intel 8086 processor processor • 768KB memory • 20MB disk • Dot-Matrix printer (9-pin)
  • 17. More advanced architectures • Pipeline SIMD • SIMD • Multi-core • Cache
  • 20. My computers Desktop (Intel Pentium D 3GHz Nvidia 7900) VAIO Z46TD (I l C 2 D P9700 2 8GH ) 3GHz, Nvidia 7900) (Intel Core 2 Duo P9700 2.8GHz) iPhone 3GS (ARM Cortex-A8 GBA SP 833MHz) GBA SP (ARM7 16.78MHz)
  • 21. Computer Organization and Assembly language • It is not only about assembly but also about “computer organization” computer organization . • It will cover – Basic concept of computer systems and architecture – ARM architecture and assembly language – x86 architecture and assembly language
  • 23. TOY machine • Starting from a simple construct
  • 24. TOY machine • Build several components and connect them together together
  • 25. TOY machine • Almost as good as any computers
  • 26. TOY machine A DUP 32 int A[32]; 10: C020 lda R1, 1 lda RA, A 20: 7101 21: 7A00 lda RC, 0 d ld RD 0 FF i=0; Do { RD tdi 22: 7C00 23 8DFF read ld RD, 0xFF bz RD, exit add R2 RA RC RD=stdin; if (RD==0) break; 23: 8DFF 24: CD29 25: 12AC add R2, RA, RC sti RD, R2 add RC, RC, R1 A[i]=RD; i=i+1; 25: 12AC 26: BD02 27: 1CC1 bz R0, read it jl RF i t } while (1); i t () 28: C023 29 FF2B exit jl RF, printr hlt printr(); 29: FF2B 2A: 0000
  • 27. ARM • ARM architecture ARM bl i • ARM assembly programming
  • 28. IA32 • IA-32 Processor Architecture • Data Transfers Addressing and Arithmetic • Data Transfers, Addressing, and Arithmetic • Procedures • Conditional Processing g • Integer Arithmetic • Advanced Procedures • Strings and Arrays • High-Level Language Interface • Real Arithmetic (FPU) • SIMD • Code Optimization
  • 29. What you will learn • Basic principle of computer architecture H k • How your computer works • How your C programs work • Assembly basics • ARM assembly programming • ARM assembly programming • IA-32 assembly programming S ifi t FPU/MMX • Specific components, FPU/MMX • Code optimization • Interface between assembly to high-level language g g
  • 30. Why taking this course? • Does anyone really program in assembly nowadays? nowadays? Yes at times you do need to write assembly • Yes, at times, you do need to write assembly code. • It is foundation for computer architecture and • It is foundation for computer architecture and compilers. It is related to electronics, logic design and operating system design and operating system.
  • 31. CSIE courses • Hardware: electronics, digital system, architecture architecture • Software: operating system, compiler
  • 32. wikipedia • Today, assembly language is used primarily for direct hardware manipulation access to direct hardware manipulation, access to specialized processor instructions, or to address critical performance issues Typical uses critical performance issues. Typical uses are device drivers, low-level embedded systems, and real time systems and real-time systems.
  • 33. Reasons for not using assembly • Development time: it takes much longer to develop in assembly Harder to debug no type develop in assembly. Harder to debug, no type checking, side effects… M i t i bilit t t d di t t i k • Maintainability: unstructured, dirty tricks • Portability: platform-dependent
  • 34. Reasons for using assembly • Educational reasons: to understand how CPUs and compilers work Better understanding to and compilers work. Better understanding to efficiency issues of various constructs. D l i il d b d th • Developing compilers, debuggers and other development tools. • Hardware drivers and system code • Embedded systems y • Developing libraries. • Accessing instructions that are not available • Accessing instructions that are not available through high-level languages. O ti i i f d • Optimizing for speed or space
  • 35. To sum up • It is all about lack of smart compilers • Faster code, compiler is not good enough • Smaller code , compiler is not good enough, e.g. mobile devices, embedded devices, also , , Smaller code → better cache performance → faster code • Unusual architecture , there isn’t even a compiler or compiler quality is bad eg GPU compiler or compiler quality is bad, eg GPU, DSP chips, even MMX.
  • 36. Overview • Virtual Machine Concept p • Data Representation • Boolean Operations • Boolean Operations
  • 37. Translating languages English: Display the sum of A times B plus C English: Display the sum of A times B plus C. C++: cout << (A * B + C); cout << (A B + C); Intel Machine Language: Assembly Language: mov eax,A Intel Machine Language: A1 00000000 F7 25 00000004 mul B add eax,C ll W it I t F7 25 00000004 03 05 00000008 E8 00500000 call WriteInt E8 00500000
  • 38. Virtual machines Abstractions for computers High-Level Language Level 5 Assembly Language Level 4 Operating System Instruction Set Level 3 Architecture Microarchitecture Level 1 Level 2 Digital Logic Level 0
  • 39. High-level language • Level 5 • Application-oriented languages • Programs compile into assembly language Programs compile into assembly language (Level 4) cout << (A * B + C);
  • 40. Assembly language • Level 4 • Instruction mnemonics that have a one-to-one correspondence to machine language • Calls functions written at the operating system level (Level 3) y ( ) • Programs are translated into machine language (Level 2) language (Level 2) mov eax, A mul B mul B add eax, C call WriteInt
  • 41. Operating system • Level 3 • Provides services • Programs translated and run at the instruction g set architecture level (Level 2)
  • 42. Instruction set architecture • Level 2 • Also known as conventional machine language • Executed by Level 1 program y p g (microarchitecture, Level 1) A1 00000000 F7 25 00000004 03 05 00000008 E8 00500000
  • 43. Microarchitecture • Level 1 • Interprets conventional machine instructions (Level 2) • Executed by digital hardware (Level 0)
  • 44. Digital logic • Level 0 CPU d f di i l l i • CPU, constructed from digital logic gates • System bus • Memory
  • 45. Data representation • Computer is a construction of digital circuits with two states: on and off with two states: on and off • You need to have the ability to translate b t diff t t ti t i between different representations to examine the content of the machine • Common number systems: binary, octal, decimal and hexadecimal
  • 46. Binary representations • Electronic Implementation E t t ith bi t bl l t – Easy to store with bistable elements – Reliably transmitted on noisy and inaccurate wires 0 1 0 2.8V 3.3V 0.0V 0.5V
  • 47. Binary numbers • Digits are 1 and 0 ( bi di it i ll d bit) (a binary digit is called a bit) 1 = true 0 = false • MSB –most significant bit • LSB –least significant bit MSB LSB • Bit numbering: 1 0 1 1 0 0 1 0 1 0 0 1 1 1 0 0 MSB LSB A bit string could have different interpretations 0 15 • A bit string could have different interpretations
  • 48. Unsigned binary integers • Each digit (bit) is either 1 or 0 • Each bit represents a power of 2: 1 1 1 1 1 1 1 1 27 26 25 24 23 22 21 20 Every binary number is a f sum of powers of 2
  • 49. Translating binary to decimal Weighted positional notation shows how to Weighted positional notation shows how to calculate the decimal value of each binary bit: d (D 2n 1) (D 2n 2) (D 21) (D dec = (Dn-1  2n-1)  (Dn-2  2n-2)  ...  (D1  21)  (D0  20) D = binary digit binary 00001001 = decimal 9: (1 23) (1 20) 9 (1  23) + (1  20) = 9
  • 50. Translating unsigned decimal to binary • Repeatedly divide the decimal integer by 2. Each remainder is a binary digit in the translated value: remainder is a binary digit in the translated value: 37 = 100101 37 = 100101
  • 51. Binary addition • Starting with the LSB, add each pair of digits, include the carry if present include the carry if present. 1 carry: 0 0 0 0 0 1 0 0 1 (4) carry: 0 0 0 0 0 1 1 1 + (7) 0 0 0 0 1 0 1 1 0 0 0 0 1 0 1 1 (11) 0 1 2 3 4 bit position: 5 6 7 0 1 2 3 4 bit position: 5 6 7
  • 52. Integer storage sizes byte 16 8 Standard sizes: 16 32 word doubleword 64 quadword Standard sizes: 64 quadword Practice: What is the largest unsigned integer that may be stored in 20 bits? Practice: What is the largest unsigned integer that may be stored in 20 bits?
  • 53. Large measurements • Kilobyte (KB), 210 bytes M b (MB) 220 b • Megabyte (MB), 220 bytes • Gigabyte (GB), 230 bytes • Terabyte (TB), 240 bytes • Petabyte • Petabyte • Exabyte Z tt b t • Zettabyte • Yottabyte
  • 54. Hexadecimal integers All values in memory are stored in binary. Because long binary numbers are hard to read we use hexadecimal binary numbers are hard to read, we use hexadecimal representation.
  • 55. Translating binary to hexadecimal • Each hexadecimal digit corresponds to 4 binary bits. • Example: Translate the binary integer • Example: Translate the binary integer 000101101010011110010100 to hexadecimal:
  • 56. Converting hexadecimal to decimal • Multiply each digit by its corresponding f 16 power of 16: dec = (D3  163) + (D2  162) + (D1  161) + (D0  160) H 1234 l (1 163) + (2 162) + (3 161) + (4 • Hex 1234 equals (1  163) + (2  162) + (3  161) + (4  160), or decimal 4,660. • Hex 3BA4 equals (3  163) + (11 * 162) + (10  161) Hex 3BA4 equals (3  16 ) + (11 16 ) + (10  16 ) + (4  160), or decimal 15,268.
  • 57. Powers of 16 Used when calculating hexadecimal values up to 8 digits long:
  • 58. Converting decimal to hexadecimal decimal 422 = 1A6 hexadecimal
  • 59. Hexadecimal addition Divide the sum of two digits by the number base (16) Th ti t b th l d (16). The quotient becomes the carry value, and the remainder is the sum digit. 36 28 28 6A 1 1 36 28 28 6A 42 45 58 4B 78 6D 80 B5 78 6D 80 B5 Important skill: Programmers frequently add and subtract the addresses of variables and instructions addresses of variables and instructions.
  • 60. Hexadecimal subtraction When a borrow is required from the digit to the l ft dd 10h t th t di it' l left, add 10h to the current digit's value: C6 75 1 A2 47 24 2E Practice: The address of var1 is 00400020. The address of the next variable after var1 is 0040006A How many bytes are used by var1? variable after var1 is 0040006A. How many bytes are used by var1?
  • 61. Signed integers The highest bit indicates the sign. 1 = negative, 0 i i 0 = positive sign bit sign bit 1 1 1 1 0 1 1 0 Negative 0 0 0 0 1 0 1 0 Positive If the highest digit of a hexadecmal integer is > 7, the value is negative Examples: 8A C5 A2 9D negative. Examples: 8A, C5, A2, 9D
  • 62. Two's complement notation Steps: Complement (reverse) each bit – Complement (reverse) each bit – Add 1 Note that 00000001 + 11111111 = 00000000
  • 63. Binary subtraction • When subtracting A – B, convert B to its two's complement complement • Add A to (–B) 0 1 0 1 0 0 1 0 1 0 – 0 1 0 1 1 1 0 1 0 0 1 1 1 1 1 Advantages for 2’s complement: Advantages for 2’s complement: • No two 0’s • Sign bit • Remove the need for separate circuits for add and sub
  • 64. Ranges of signed integers The highest bit is reserved for the sign. This limits the range: the range:
  • 65. Character • Character sets St d d ASCII(0 127) – Standard ASCII(0 – 127) – Extended ASCII (0 – 255) ANSI (0 255) – ANSI (0 – 255) – Unicode (0 – 65,535) • Null-terminated String – Array of characters followed by a null byte • Using the ASCII table – back inside cover of book
  • 66. Representing Instructions int sum(int x, int y) { PC sum Alpha sum Sun sum { return x+y; } 55 89 00 00 p 81 C3 – For this example, Alpha & Sun use two 4-byte E5 8B 45 30 42 01 E0 08 90 instructions • Use differing numbers of instructions in other cases 0C 03 45 80 FA 6B 02 00 09 instructions in other cases – PC uses 7 instructions with lengths 1, 2, and 3 08 89 EC Diff t hi t t ll diff t g , , bytes • Same for NT and for Linux EC 5D C3 Different machines use totally different instructions and encodings • NT / Linux not fully binary compatible
  • 67. Boolean algebra • Boolean expressions created from: – NOT, AND, OR
  • 68. NOT • Inverts (reverses) a boolean value • Truth table for Boolean NOT operator: Digital gate diagram for NOT: N O T N O T
  • 69. AND • Truth if both are true • Truth table for Boolean AND operator: Digital gate diagram for AND: AND
  • 70. OR • True if either is true • Truth table for Boolean OR operator: Digital gate diagram for OR: O R
  • 71. Operator precedence • NOT > AND > OR • Examples showing the order of operations: • Use parentheses to avoid ambiguity Use parentheses to avoid ambiguity
  • 72. Implementation of gates • Fluid switch (http://www.cs.princeton.edu/introcs/lectures/fluid-computer.swf)
  • 75. Truth Tables (1 of 2) • A Boolean function has one or more Boolean i d i l B l inputs, and returns a single Boolean output. • A truth table shows all the inputs and outputs of a Boolean function Example: X  Y
  • 76. Truth Tables (2 of 2) • Example: X  Y