CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut [email protected] CSC231...

81
mith College Computer Science Dominique Thiébaut [email protected] CSC231 - Assembly Week #8

Transcript of CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut [email protected] CSC231...

Page 1: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

mith College

Computer Science

Dominique Thiébaut [email protected]

CSC231 - AssemblyWeek #8

Page 2: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

https://xkcd.com/571/

Can't Sleep

Page 3: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Logic Design

Page 4: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Logic Design

Page 5: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Image from http://www.willegal.net/appleii/images/motherboard.jpg

Apple II Motherboard

Page 6: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Nasa Computer(Apollo)

Page 7: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 8: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

https://www.youtube.com/watch?v=xTQDIiSWK_k

Page 9: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Designing a 2-bitAdder with Logic

Page 10: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Page 11: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

ALU

CU

Processor

Page 12: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

ALU

yixi

sumi

carryi

Compute add x, y

Page 13: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

yx

z

x0 y0x1 y1x2 y2xn yn x3 y3

z0z1z2z3zn

Page 14: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 15: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 16: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 17: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

x0

z0

XORAND

y0

carry

Page 18: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Moral of the Story: Addition is performed

by logic operations using natural binary numbers…

(unsigned arithmetic)

Page 19: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

NEGATIVE NUMBERS

Page 20: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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…

Page 21: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 22: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 23: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 24: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Signed MagnitudeNumber System

Page 25: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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…

Page 26: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 27: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 28: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

1's ComplementNumber System

Page 29: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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…

Page 30: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 31: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 32: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

2's ComplementNumber System

Page 33: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 34: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 35: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 36: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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?

Page 37: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

int x = 0x7fffffff - 5;

for ( int i=0; i<10; i++ )System.out.println( x++ );

getcopy Loop0x7fffffff.java

Page 38: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

What did you just learn about Java ints?

Page 39: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 40: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 41: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 42: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #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)

Page 43: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

The 13th Floorhttps://www.youtube.com/watch?v=dtYdZkPmFoU

Page 44: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

The LOOP Instruction

Page 45: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 46: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 47: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 48: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 49: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Tracinga Code Example

Page 50: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 51: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 52: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 53: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 54: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 55: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 56: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 57: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 58: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 59: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 60: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 61: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 62: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 63: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Example 1Sum of 1..10

Page 64: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 65: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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?

Page 66: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 67: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 68: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Looping Through

Arrays

Page 69: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Looping Through

Arrays

LOOP INSTRUCTION

INDIRECTADDRESSINGMODE

Page 70: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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.

Page 71: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Tracing One Example

of Indirect Addressing (Base Addressing)

Page 72: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 73: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 74: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 75: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 76: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 77: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 78: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

Example 2:Setting an Array

to All 0s

Page 79: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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

Page 80: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

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.

Page 81: CSC231 - Assembly · mith College C omputer Science Dominique Thiébaut dthiebaut@smith.edu CSC231 - Assembly Week #8

D. Thiebaut, Computer Science, Smith College

We stopped here last time…