Upload
others
View
12
Download
0
Embed Size (px)
Citation preview
mith College
Computer Science
Dominique Thiébaut [email protected]
CSC231 - AssemblyWeek #6 Fall 2019
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?)
D. Thiebaut, Computer Science, Smith College
More ArithmeticInstructions
D. Thiebaut, Computer Science, Smith College
ALUALU
D. Thiebaut, Computer Science, Smith College
ALUALU
Right now, we are dealing only
with UNSIGNED integers!
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
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
D. Thiebaut, Computer Science, Smith College
mulmul reg8mul reg16mul reg32mul mem8mul mem16mul mem32
mul operand
D. Thiebaut, Computer Science, Smith College
Observation
1001x 1110
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
D. Thiebaut, Computer Science, Smith College
This has tremendously important consequences!
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(); }}
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
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
D. Thiebaut, Computer Science, Smith College
Java's Attitude
Pentium
int x, y, z; x = … y = … z = x * y;
ALUmul
x
y
z
RAM
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
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
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
D. Thiebaut, Computer Science, Smith College
Same Computation in Python…
D. Thiebaut, Computer Science, Smith College
Same Computation in Python…
Interpreted
vs. Compiled
Languages
D. Thiebaut, Computer Science, Smith College
How big is a 32-bit int?
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
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
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
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
D. Thiebaut, Computer Science, Smith College
Exercise
alpha db 3beta dw 4x dd 6
Compute x = 2*alpha +3*beta + x - 1
D. Thiebaut, Computer Science, Smith College
D. Thiebaut, Computer Science, Smith College
Logical Instructions
AND, OR, NOT, XOR
D. Thiebaut, Computer Science, Smith College
Is (x,y) inside Rectangle?
x1,y1
x2,y2
D. Thiebaut, Computer Science, Smith College
a b a and bF F FF T FT F FT T T
Truth Table
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
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
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
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
D. Thiebaut, Computer Science, Smith College
10010and 11100
10010 or 11100
10010xor 11100
not 11100
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
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
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
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
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
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
D. Thiebaut, Computer Science, Smith College
We Stopped Here Last Time…