csc231 - assembly · mith college c omputer science dominique thiébaut [email protected] csc231...
TRANSCRIPT
D. Thiebaut, Computer Science, Smith College
https://xkcd.com/571/
Can't Sleep
D. Thiebaut, Computer Science, Smith College
Logic Design
D. Thiebaut, Computer Science, Smith College
Logic Design
D. Thiebaut, Computer Science, Smith College
Image from http://www.willegal.net/appleii/images/motherboard.jpg
Apple II Motherboard
D. Thiebaut, Computer Science, Smith College
Nasa Computer(Apollo)
D. Thiebaut, Computer Science, Smith College
A Bit of History
• Claude Shannon • 21 years old • 1937 • MIT Master's Thesis • All arithmetic operations
on binary numbers can be performed usingboolean logic operators
https://en.wikipedia.org/wiki/Claude_Shannon
D. Thiebaut, Computer Science, Smith College
https://www.youtube.com/watch?v=xTQDIiSWK_k
D. Thiebaut, Computer Science, Smith College
Designing a 2-bitAdder with Logic
D. Thiebaut, Computer Science, Smith College
D. Thiebaut, Computer Science, Smith College
ALU
CU
Processor
D. Thiebaut, Computer Science, Smith College
ALU
yixi
sumi
carryi
Compute add x, y
D. Thiebaut, Computer Science, Smith College
yx
z
…
x0 y0x1 y1x2 y2xn yn x3 y3
z0z1z2z3zn
D. Thiebaut, Computer Science, Smith College
x0y0
z0
0
+ 0 ____
= 0 0
0+ 1____
= 0 1
1+ 0____
= 0 1
1+ 1____
= 1 0
carry
D. Thiebaut, Computer Science, Smith College
x0 y0 Carry z0
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0
D. Thiebaut, Computer Science, Smith College
x0 y0 Carry z0
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0
Carry = x0 and y0
z0 = x0 xor y0
D. Thiebaut, Computer Science, Smith College
x0
z0
XORAND
y0
carry
D. Thiebaut, Computer Science, Smith College
Moral of the Story: Addition is performed
by logic operations using natural binary numbers…
(unsigned arithmetic)
D. Thiebaut, Computer Science, Smith College
NEGATIVE NUMBERS
D. Thiebaut, Computer Science, Smith College
How can we represent signed binary numbers when
all we have are bits (0/1)?
Whichever system we use should work
with the binary adder in the ALU…
D. Thiebaut, Computer Science, Smith College
Binary Hex Unsigned Decimal
0000 0 00001 1 10010 2 20011 3 30100 4 40101 5 50110 6 60111 7 71000 8 81001 9 91010 A 101011 B 111100 C 121101 D 131110 E 141111 F 15
4-bit Nybble
D. Thiebaut, Computer Science, Smith College
Binary Hex Unsigned Decimal
0 000 0 00 001 1 10 010 2 20 011 3 30 100 4 40 101 5 50 110 6 60 111 7 71 000 8 81 001 9 91 010 A 10 1 011 B 111 100 C 121 101 D 131 110 E 141 111 F 15
4-bit NybbleSign Bit
D. Thiebaut, Computer Science, Smith College
Binary Hex Unsigned Decimal
0 000 0 00 001 1 10 010 2 20 011 3 30 100 4 40 101 5 50 110 6 60 111 7 71 000 8 81 001 9 91 010 A 10 1 011 B 111 100 C 121 101 D 131 110 E 141 111 F 15
PositiveNumbers
NegativeNumbers
4-bit Nybble
D. Thiebaut, Computer Science, Smith College
Signed MagnitudeNumber System
D. Thiebaut, Computer Science, Smith College
Signed Magnitude Rule: To find the opposite of a number, just flip
its MSB
0 101 (+5)
1 101(-5)
and vice versa…
D. Thiebaut, Computer Science, Smith College
Binary Hex Unsigned Decimal
Signed Magnitude
0 000 0 0 +00 001 1 1 +10 010 2 2 +20 011 3 3 +30 100 4 4 +40 101 5 5 +50 110 6 6 +60 111 7 7 +71 000 8 8 -01 001 9 9 -11 010 A 10 -2 1 011 B 11 -31 100 C 12 -41 101 D 13 -51 110 E 14 -61 111 F 15 -7
4-bit Nybble
D. Thiebaut, Computer Science, Smith College
Does this System work With the ALU Adder?
Binary Hex Unsigned Decimal
Signed Magnitud
e0 000 0 0 +00 001 1 1 +10 010 2 2 +20 011 3 3 +30 100 4 4 +40 101 5 5 +50 110 6 6 +60 111 7 7 +71 000 8 8 -01 001 9 9 -11 010 A 10 -2 1 011 B 11 -31 100 C 12 -41 101 D 13 -51 110 E 14 -61 111 F 15 -7
3 + -3——- = 0
4 + -1——- = 3
D. Thiebaut, Computer Science, Smith College
1's ComplementNumber System
D. Thiebaut, Computer Science, Smith College
1's Complement Rule: To find the opposite of a number,
just flip all its bits
0 101 (+5)
1 010(-5)
and vice versa…
D. Thiebaut, Computer Science, Smith College
Binary Hex Unsigned Decimal
1'sComplement
0 000 0 0 +00 001 1 1 +10 010 2 2 +20 011 3 3 +30 100 4 4 +40 101 5 5 +50 110 6 6 +60 111 7 7 +71 000 8 8 -71 001 9 9 -61 010 A 10 -5 1 011 B 11 -41 100 C 12 -31 101 D 13 -21 110 E 14 -11 111 F 15 -0
4-bit Nybble
D. Thiebaut, Computer Science, Smith College
Does this System work With the ALU Adder?
Binary Hex Unsigned Decimal
1'sComplement
0 000 0 0 +00 001 1 1 +10 010 2 2 +20 011 3 3 +30 100 4 4 +40 101 5 5 +50 110 6 6 +60 111 7 7 +71 000 8 8 -71 001 9 9 -61 010 A 10 -5 1 011 B 11 -41 100 C 12 -31 101 D 13 -21 110 E 14 -11 111 F 15 -0
3 + -3——- = 0
4 + -1——- = 3
5 + -3——- = 2
D. Thiebaut, Computer Science, Smith College
2's ComplementNumber System
D. Thiebaut, Computer Science, Smith College
2's Complement Rule: To find the opposite of a number,
just flip all its bits, and add 1
0 101(+5)
1 011(-5)
and vice versa…
1 010 + 1
0 100 + 1
D. Thiebaut, Computer Science, Smith College
Binary Hex Unsigned Decimal
2'sComplement
0 000 0 0 +00 001 1 1 +10 010 2 2 +20 011 3 3 +30 100 4 4 +40 101 5 5 +50 110 6 6 +60 111 7 7 +71 000 8 8 -81 001 9 9 -71 010 A 10 -6 1 011 B 11 -51 100 C 12 -41 101 D 13 -31 110 E 14 -21 111 F 15 -1
4-bit Nybble
D. Thiebaut, Computer Science, Smith College
Does this System workWith the ALU Adder?
Binary Hex Unsigned Decimal
2'sComplement
0 000 0 0 +00 001 1 1 +10 010 2 2 +20 011 3 3 +30 100 4 4 +40 101 5 5 +50 110 6 6 +60 111 7 7 +71 000 8 8 -81 001 9 9 -71 010 A 10 -6 1 011 B 11 -51 100 C 12 -41 101 D 13 -31 110 E 14 -31 111 F 15 -1
3 + -3——- = 0
4 + -1 ——- = 3
5 + -3——- = 2
D. Thiebaut, Computer Science, Smith College
InterestingProperty
• What is the binary representation of -1 as a byte?
• What is the binary representation of -1 as a word?
• What is the binary representation of -1 as a dword?
D. Thiebaut, Computer Science, Smith College
int x = 0x7fffffff - 5;
for ( int i=0; i<10; i++ )System.out.println( x++ );
getcopy Loop0x7fffffff.java
D. Thiebaut, Computer Science, Smith College
What did you just learn about Java ints?
D. Thiebaut, Computer Science, Smith College
negneg op8 neg op16 neg op32 op: mem, reg
alpha db 1beta dw 4x dd 0xF06
neg byte[alpha] mov ax,1234 neg ax
neg dword[x]
neg oprnd
To get the
2's complement
of an int
D. Thiebaut, Computer Science, Smith College
Range of 2's Comp't. intsBinary Hex Unsigned
Decimal0000 0 00001 1 10010 2 20011 3 30100 4 40101 5 50110 6 60111 7 71000 8 81001 9 91010 A 101011 B 111100 C 121101 D 131110 E 141111 F 15
D. Thiebaut, Computer Science, Smith College
Type Minimum Maximum # Bytes
unsigned byte 0 255 1
signed byte -128 127 1
unsigned short 0 65,535 2
signed short -32,768 32,767 2
unsigned int 0 4,294,967,295 4
signed int -2,147,483,648 2,147,483,647 4
signed long
−9,223,372,036,854,775,808
9,223,372,036,854,775,807 8
We Stopped Here Last Time
http://www.genengnews.com/media/images/genhighlight/Mar12_2014_8296129_StopSigns_GeronTrialHalt2322361121.jpg
(and finished first exercise on next slide)
D. Thiebaut, Computer Science, Smith College
The 13th Floorhttps://www.youtube.com/watch?v=dtYdZkPmFoU
D. Thiebaut, Computer Science, Smith College
The LOOP Instruction
D. Thiebaut, Computer Science, Smith College
looploop label
x dd 1sum dd 0 mov ecx, 10addUp: mov eax, dword[x] add dword[sum], eax inc dword[x] loop addUp ;ecx<—ecx-1 ;if ecx!=0, ; goto addUp
loop label
D. Thiebaut, Computer Science, Smith College
looploop label
x dd 1sum dd 0 mov ecx, 10addUp: mov eax, dword[x] add dword[sum], eax inc dword[x] loop addUp ;ecx<—ecx-1 ;if ecx!=0, ; goto addUp
loop label
Label
D. Thiebaut, Computer Science, Smith College
looploop label ;ecx <— ecx-1 ;if ecx!= 0, ; goto label ;else continue
x dd 1sum dd 0 mov ecx, 10addUp: mov eax, dword[x] add dword[sum], eax inc dword[x] loop addUp ;ecx<—ecx-1 ;if ecx!=0, ; goto addUp
loop label
Label
Labels _start: mov eax, 4
mov ecx, 10 for1: … … loop for1 for2: … …
loop for2
• Start with a letter (or a dot)
• End with a colon (when declared)
• Represent an address in the code section
• Must be unique in program
D. Thiebaut, Computer Science, Smith College
Tracinga Code Example
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ;if ecx!=0, ; goto for
eax
ecx
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ;if ecx!=0, ; goto for
?eax
3ecx
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ;if ecx!=0, ; goto for
1eax
3ecx
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ;if ecx!=0, ; goto for
1eax
3ecx
1
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ;if ecx!=0, ; goto for
1 2eax
3ecx
1
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ;if ecx!=0, ; goto for
1 2eax
3 2ecx
1
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ;if ecx!=0, ; goto for
1 2eax
3 2ecx
12
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ;if ecx!=0, ; goto for
1 2 3eax
3 2ecx
12
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ;if ecx!=0, ; goto for
1 2 3eax
3 2 1ecx
12
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ;if ecx!=0, ; goto for
1 2 3eax
3 2 1ecx
123
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ;if ecx!=0, ; goto for
1 2 3 4eax
3 2 1ecx
123
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ;if ecx!=0, ; goto for
1 2 3 4eax
3 2 1 0ecx
123
D. Thiebaut, Computer Science, Smith College
mov ecx, 3 mov eax, 1for: call _printDec inc eax loop for ;ecx<—ecx-1 ???? ;if ecx!=0, ; goto for
1 2 3 4eax
3 2 1 0ecx
123
D. Thiebaut, Computer Science, Smith College
Example 1Sum of 1..10
D. Thiebaut, Computer Science, Smith College
; computes sum(1,2, …10)x dd 1sum dd 0 mov ecx, 10 mov eax, dword[x]addUP: add dword[sum], eax inc eax loop addUp ;ecx<—ecx-1 ;if ecx!=0, ; goto addUp mov eax, dword[sum] call _printDec
D. Thiebaut, Computer Science, Smith College
; computes sum(1,2, …10)x dd 1sum dd 0 mov ecx, 10 addUP: add dword[sum], ecx loop addUp ;ecx<—ecx-1 ;if ecx!=0, ; goto addUp mov eax, dword[sum] call _printDec
Do we needeax?
D. Thiebaut, Computer Science, Smith College
Example 2Fibonaccis
http://i.dailymail.co.uk/i/pix/2016/07/17/13/1A32203E000005DC-3694326-image-m-17_1468760305397.jpg
D. Thiebaut, Computer Science, Smith College
_start: mov eax, 1 ; fibn mov ebx, 1 ; fibn-1 call _printDec call _println
mov ecx, 10-1 ; we printed 1, 9 more to go for: mov edx, ebx mov ebx, eax add eax, edx call _printDec call _println loop for
getcopy fib.asm
; print the first 10 Fibonacci terms
D. Thiebaut, Computer Science, Smith College
Looping Through
Arrays
D. Thiebaut, Computer Science, Smith College
Looping Through
Arrays
LOOP INSTRUCTION
INDIRECTADDRESSINGMODE
D. Thiebaut, Computer Science, Smith College
Indirect AddressingMode
The addressing mode refers to the way the operand of an instruction is generated. We already know register mode, immediate mode, and direct mode.
D. Thiebaut, Computer Science, Smith College
Tracing One Example
of Indirect Addressing (Base Addressing)
D. Thiebaut, Computer Science, Smith College
12
33
78
56
3E
F0
3
1
Memory
0x110450x110460x110470x110480x110490x1104A0x1104B0x1104C
???ebx?al
section .data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12
section .text
_start: mov al, 'z' mov ebx, A mov byte[ebx], 0
mov ebx, B mov byte[ebx], al
D. Thiebaut, Computer Science, Smith College
12
33
78
56
3E
F0
3
1
Memory
0x110450x110460x110470x110480x110490x1104A0x1104B0x1104C
section .data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12
section .text
_start: mov al, 'z' mov ebx, A mov byte[ebx], 0
mov ebx, B mov byte[ebx], al
???ebx'z'al
D. Thiebaut, Computer Science, Smith College
12
33
78
56
3E
F0
3
1
Memory
0x110450x110460x110470x110480x110490x1104A0x1104B0x1104C
section .data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12
section .text
_start: mov al, 'z' mov ebx, A mov byte[ebx], 0
mov ebx, B mov byte[ebx], al
11045ebx'z'al
D. Thiebaut, Computer Science, Smith College
12
33
78
56
3E
F0
3
1 0
Memory
0x110450x110460x110470x110480x110490x1104A0x1104B0x1104C
section .data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12
section .text
_start: mov al, 'z' mov ebx, A mov byte[ebx], 0
mov ebx, B mov byte[ebx], al
11045ebx'z'al
D. Thiebaut, Computer Science, Smith College
12
33
78
56
3E
F0
3
1 0
Memory
0x110450x110460x110470x110480x110490x1104A0x1104B0x1104C
section .data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12
section .text
_start: mov al, 'z' mov ebx, A mov byte[ebx], 0
mov ebx, B mov byte[ebx], al
1104Aebx'z'al
D. Thiebaut, Computer Science, Smith College
12
33
78 'z'
56
3E
F0
3
1 0
Memory
0x110450x110460x110470x110480x110490x1104A0x1104B0x1104C
section .data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12
section .text
_start: mov al, 'z' mov ebx, A mov byte[ebx], 0
mov ebx, B mov byte[ebx], al
1104Aebx'z'al
D. Thiebaut, Computer Science, Smith College
Example 2:Setting an Array
to All 0s
D. Thiebaut, Computer Science, Smith College
; Array Table contains 10 wordsTable dw 1,2,3,4,5,6 dw 7,8,9,10 mov ecx, ____ ;# of elements mov ebx, ____ ;address of ;Tableclear: mov word[ebx], ____;value to store add ebx,____ ;make ebx point ;to next word loop clear ;ecx<—ecx-1 ;if ecx!=0, ; goto clear
D. Thiebaut, Computer Science, Smith College
ExercisesProblem #1: Store the first 10 Fibonacci terms in an array of ints (32 bits)
Problem #2: Given a DNA sequence of 1,000,000 characters stored in an array of bytes, and all characters in uppercase, transform it into its lowercase equivalent. The characters are A, C, G, T and N.
D. Thiebaut, Computer Science, Smith College
We stopped here last time…