CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al...
Transcript of CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al...
![Page 1: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/1.jpg)
mith College
Computer Science
Dominique Thiébaut [email protected]
CSC231 - AssemblyWeek #6 Fall 2019
![Page 2: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/2.jpg)
D. Thiebaut, Computer Science, Smith College
Outline
• More arithmetic instructions
• MUL is special…
• DIV is too!
• Mixing data types
• Logical Instructions (AND, OR, NOT, XOR)
• Negative Numbers (next week?)
![Page 3: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/3.jpg)
D. Thiebaut, Computer Science, Smith College
More ArithmeticInstructions
![Page 4: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/4.jpg)
D. Thiebaut, Computer Science, Smith College
ALUALU
![Page 5: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/5.jpg)
D. Thiebaut, Computer Science, Smith College
ALUALU
![Page 6: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/6.jpg)
Right now, we are dealing only
with UNSIGNED integers!
![Page 7: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/7.jpg)
D. Thiebaut, Computer Science, Smith College
incinc reg8inc reg16inc reg32inc mem8inc mem16inc mem32
alpha db 3beta dw 4x dd 0
inc al inc cx inc ebx
inc word[beta] ;beta <- 5 inc dword[x] ;x <- 1
inc operand
![Page 8: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/8.jpg)
D. Thiebaut, Computer Science, Smith College
decdec reg8dec reg16dec reg32dec mem8dec mem16dec mem32
alpha db 3beta dw 4x dd 6
dec al ;al <- al-1 dec cx dec ebx
dec word[beta] ;beta <- 3 dec dword[x] ;x <- 5
dec operand
![Page 9: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/9.jpg)
D. Thiebaut, Computer Science, Smith College
mulmul reg8mul reg16mul reg32mul mem8mul mem16mul mem32
mul operand
![Page 10: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/10.jpg)
D. Thiebaut, Computer Science, Smith College
Observation
1001x 1110
![Page 11: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/11.jpg)
D. Thiebaut, Computer Science, Smith College
mulmul reg8mul reg16mul reg32mul mem8mul mem16mul mem32
alpha db 3beta dw 4x dd 6
mul byte[alpha] ;ax <- al*alpha mul ebx ;edx:eax <- ; ebx*eax
mul operand edx:eax <— operand32 * eax dx:ax <— operand16 * ax ax <— operand8 * al
![Page 12: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/12.jpg)
D. Thiebaut, Computer Science, Smith College
This has tremendously important consequences!
![Page 13: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/13.jpg)
D. Thiebaut, Computer Science, Smith College
public class JavaLimits {
public static void main(String[] args) { // ----------------------------------------------- // a multiplication of ints int x = 0x30000001; int y = 0x30000001;
System.out.println( "x = " + x ); System.out.println( "y = " + y ); int z = x * y;
System.out.println( "z = " + z ); System.out.println(); }}
![Page 14: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/14.jpg)
D. Thiebaut, Computer Science, Smith College
public class JavaLimits {
public static void main(String[] args) { // ----------------------------------------------- // a multiplication of ints int x = 0x30000001; int y = 0x30000001;
System.out.println( "x = " + x ); System.out.println( "y = " + y ); int z = x * y;
System.out.println( "z = " + z ); System.out.println(); }}
x = 805306369 y = 805306369 z = 1610612737
![Page 15: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/15.jpg)
D. Thiebaut, Computer Science, Smith College
public class JavaLimits {
public static void main(String[] args) { // ----------------------------------------------- // a multiplication of ints int x = 0x30000001; int y = 0x30000001;
System.out.println( "x = " + x ); System.out.println( "y = " + y ); int z = x * y;
System.out.println( "z = " + z ); System.out.println(); }}
x = 805306369 y = 805306369 z = 1610612737
![Page 16: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/16.jpg)
D. Thiebaut, Computer Science, Smith College
Java's Attitude
Pentium
int x, y, z; x = … y = … z = x * y;
ALUmul
x
y
z
RAM
![Page 17: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/17.jpg)
D. Thiebaut, Computer Science, Smith College
Java's Attitude
Pentium
int x, y, z; x = … y = … z = x * y;
ALUmul
x
y
z
RAM
eax
eaxedx
![Page 18: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/18.jpg)
D. Thiebaut, Computer Science, Smith College
Java's Attitude
Pentium
int x, y, z; x = … y = … z = x * y;
ALUmul
x
y
z
RAM
eax
eaxedx
![Page 19: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/19.jpg)
D. Thiebaut, Computer Science, Smith College
Java's Attitude
Pentium
int x, y, z; x = … y = … z = x * y;
ALUmul
x
y
z
RAM
eax
eaxedx
![Page 20: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/20.jpg)
D. Thiebaut, Computer Science, Smith College
Same Computation in Python…
![Page 21: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/21.jpg)
D. Thiebaut, Computer Science, Smith College
Same Computation in Python…
Interpreted
vs. Compiled
Languages
![Page 22: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/22.jpg)
D. Thiebaut, Computer Science, Smith College
How big is a 32-bit int?
![Page 23: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/23.jpg)
D. Thiebaut, Computer Science, Smith College
Ranges (Unsigned Integers)
8 bits 0 - 255 16 bits 0 - 65,535 32 bits 0 - 4,294,967,295
![Page 24: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/24.jpg)
D. Thiebaut, Computer Science, Smith College
Puzzling Behavior
int x = 1;
for (int i=0; i<40; i++ ) { System.out.println( x ); x = x * 2;}
What is the output?
getcopy JavaMulBy2.java
![Page 25: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/25.jpg)
D. Thiebaut, Computer Science, Smith College
mulmul reg8mul reg16mul reg32mul mem8mul mem16mul mem32
alpha db 3beta dw 4x dd 6
mul byte[alpha] ;ax <- al*alpha mul ebx ;edx:eax <- ; ebx*eax
mul operand edx:eax <— operand32 * eax dx:ax <— operand16 * ax ax <— operand8 * al
REVIEW
![Page 26: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/26.jpg)
D. Thiebaut, Computer Science, Smith College
divdiv reg8div reg16div reg32div mem8div mem16div mem32
alpha db 3beta dw 4x dd 6;compute beta/alpha mov ax, word[beta] div byte[alpha];quotient in al;remainder in ah
div operand R : Q edx:eax <— edx:eax / operand32 dx:ax <— dx:ax / operand16 ah:al <— ax / operand8
![Page 27: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/27.jpg)
D. Thiebaut, Computer Science, Smith College
Exercise
alpha db 3beta dw 4x dd 6
Compute x = 2*alpha +3*beta + x - 1
![Page 28: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/28.jpg)
D. Thiebaut, Computer Science, Smith College
![Page 29: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/29.jpg)
D. Thiebaut, Computer Science, Smith College
Logical Instructions
AND, OR, NOT, XOR
![Page 30: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/30.jpg)
D. Thiebaut, Computer Science, Smith College
Is (x,y) inside Rectangle?
x1,y1
x2,y2
![Page 31: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/31.jpg)
D. Thiebaut, Computer Science, Smith College
a b a and bF F FF T FT F FT T T
Truth Table
![Page 32: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/32.jpg)
D. Thiebaut, Computer Science, Smith College
a b a and bF F FF T FT F FT T T
a b a and b0 0 00 1 01 0 01 1 1
![Page 33: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/33.jpg)
D. Thiebaut, Computer Science, Smith College
a b a and bF F FF T FT F FT T T
a b a and b0 0 00 1 01 0 01 1 1
a b a or b0 0 00 1 11 0 11 1 1
![Page 34: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/34.jpg)
D. Thiebaut, Computer Science, Smith College
a b a and bF F FF T FT F FT T T
a b a and b0 0 00 1 01 0 01 1 1
a b a or b0 0 00 1 11 0 11 1 1
a b a xor b0 0 00 1 11 0 11 1 0
![Page 35: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/35.jpg)
D. Thiebaut, Computer Science, Smith College
a b a and bF F FF T FT F FT T T
a b a and b0 0 00 1 01 0 01 1 1
a b a or b0 0 00 1 11 0 11 1 1
a b a xor b0 0 00 1 11 0 11 1 0
a not a
0 1
1 0
![Page 36: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/36.jpg)
D. Thiebaut, Computer Science, Smith College
10010and 11100
10010 or 11100
10010xor 11100
not 11100
![Page 37: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/37.jpg)
D. Thiebaut, Computer Science, Smith College
Instruction Feature
AND Good for setting bits to 0
OR Good for setting bits to 1
XOR Good for flipping bits
NOT Good for complementing all the bits
Image from http://www.staugustinechico.com/wp-content/uploads/2015/05/remember.jpg
![Page 38: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/38.jpg)
D. Thiebaut, Computer Science, Smith College
10010and 11100
10010 or 11100
10010xor 11100
not 11100
10000 11110 01110
00011
Instruction Feature
AND Good for setting bits to 0
OR Good for setting bits to 1
XOR Good for flipping bits
NOT Good for complementing all the bits
![Page 39: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/39.jpg)
D. Thiebaut, Computer Science, Smith College
andand op8,op8and op16,op16and op32,op32 op: mem, reg, imm
alpha db 0xf3beta dw 4x dd 6
and byte[alpha],7 mov ax,0x1234 and ax,0xFF00
and dword[x], 2
and dest, src
![Page 40: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/40.jpg)
D. Thiebaut, Computer Science, Smith College
oror op8,op8or op16,op16or op32,op32 op: mem, reg, imm
alpha db 3beta dw 4x dw 0x0F06
or byte[alpha],4 mov ax,0x1234 or ax,0xFF00
or word[x], 15
or dest, src
![Page 41: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/41.jpg)
D. Thiebaut, Computer Science, Smith College
xorxor op8,op8xor op16,op16xor op32,op32 op: mem, reg, imm
alpha db 3beta dw 4x dd 0xF06
xor byte[alpha],0x0F mov ax,0x1234 xor ax,0xFF00
xor dword[x], 15
xor dest, src
![Page 42: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/42.jpg)
D. Thiebaut, Computer Science, Smith College
notnot op8 not op16 not op32 op: mem, reg
alpha db 3beta dw 4x dd 0xF06
not byte[alpha] mov ax,0x1234 not ax
not dword[x]
not oprnd
![Page 43: CSC231 - Assembly€¦ · inc reg32 inc mem8 inc mem16 inc mem32 alpha db 3 beta dw 4 x dd 0 inc al inc cx inc ebx inc word[beta] ;beta](https://reader036.fdocuments.in/reader036/viewer/2022070910/5f9dc2880a2ac3769365ee04/html5/thumbnails/43.jpg)
D. Thiebaut, Computer Science, Smith College
We Stopped Here Last Time…