csc231 - assembly · 2019. 10. 9. · d. thiebaut, computer science, smith college a dd 3 b dd 5...
TRANSCRIPT
mith College
Computer Science
Dominique Thiébaut [email protected]
CSC231 - AssemblyWeek #5 Fall 2019
D. Thiebaut, Computer Science, Smith College
Outline
• Working with Byte, Word and DWord Variables
• Step-by-Step Execution of a Program
• Speed of N-Queens
• Review of binary/hexadecimal/decimal
D. Thiebaut, Computer Science, Smith College
Misc. Notes & Review
D. Thiebaut, Computer Science, Smith College
Note: You Can AddA Register to Itself
; x = 2 * (a-b)
mov eax, dword[a] ; eax <- asub eax, dword[b] ; eax <- a-badd eax, eax ; eax <- eax+eax ; eax <- 2*(a-b)mov dword[x], eax ; x <- 2*(a-b)
D. Thiebaut, Computer Science, Smith College
The Pentium DataRegisters
eax
ebx
ecx
edx
ax
bx
cx
dx
ah al
bh bl
ch cl
dh dl
D. Thiebaut, Computer Science, Smith College
Review: Our Goal was…
int x, y, sum;
x = 3;y = 5;sum = x + y;
D. Thiebaut, Computer Science, Smith College
Same with Short Ints
short x, y, sum;
x = 3;y = 5;sum = x + y;
Translate this into Assembly
D. Thiebaut, Computer Science, Smith College
Same with Bytes
byte x, y, sum;
x = 3;y = 5;sum = x + y;
Translate this into Assembly
D. Thiebaut, Computer Science, Smith College
With an Array!
int A[3];
A[0] = 3;A[1] = 5;A[2] = A[0]+A[1]
Translate this into Assembly
D. Thiebaut, Computer Science, Smith College
Exerciseextern _printIntextern _println
section .dataa dw 1b dw 2c dw 3sum dw 0msg db "hello!",10
section .textglobal _start
_start:mov eax, dword[a]add eax, dword[b]add eax, dword[c]mov dword[sum],eaxcall _printIntcall _println
mov ecx, msgmov edx, 7mov eax, 4mov ebx, 1int 0x80
What is the output
of this program?
D. Thiebaut, Computer Science, Smith College
Exercisecs231a@marax:~ $ nasm -f elf week5exercise.asm cs231a@marax:~ $ ld -melf_i386 week5exercise.o 231Lib.o -o week5exercise cs231a@marax:~ $ ./week5exercise 327686 llo!
why?
D. Thiebaut, Computer Science, Smith College
Exercisesection .data
a db b db c dw d dw e dd f db
section .text
reconstruct the declarations of a, b, c, d, e
and f.
99
88
77
66
55
44
33
22
1f
1a
11
00
a
typical
midterm
question!
hex
D. Thiebaut, Computer Science, Smith College
Follow a stepby step execution
of the program
D. Thiebaut, Computer Science, Smith College
8 9 section .data 10 00000000 03000000 a dd 3 11 00000004 05000000 b dd 5 12 00000008 00000000 sum dd 0 13 14 ;;; 15 ;;; code area 16 ;;; 17 18 section .text 19 global _start
20 00000000 A1[00000000] _start: mov eax, dword[a] ;eax <-- a 21 00000005 0305[04000000] add eax, dword[b] ;eax <-- eax+b = a+b 22 0000000B 83E801 sub eax, 1 ;eax <-- eax-1 = a+b-1 23 0000000E A3[08000000] mov dword[sum], eax ;sum <-- eax = a+b-1
24 ;;; exit() 25 00000013 B801000000 mov eax,1 26 00000018 BB00000000 mov ebx,0 27 0000001D CD80 int 0x80 ; final system call
D. Thiebaut, Computer Science, Smith College
a dd 3b dd 5sum dd 0
100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax
eax
ebx
ecx
edx
eip
D. Thiebaut, Computer Science, Smith College
a dd 3b dd 5sum dd 0
100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax
eax
ebx
ecx
edx
eip
a dd 3b dd 5sum dd 0
100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax
eax
ebx
ecx
edx
eip
Tick!
D. Thiebaut, Computer Science, Smith College
a dd 3b dd 5sum dd 0
100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax
eax
ebx
ecx
edx
eip
a dd 3b dd 5sum dd 0
100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax
eax
ebx
ecx
edx
eip
a dd 3b dd 5sum dd 0
100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax
eax
ebx
ecx
edx
eip
Tick!
D. Thiebaut, Computer Science, Smith College
a dd 3b dd 5sum dd 0
100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax
eax
ebx
ecx
edx
eip
a dd 3b dd 5sum dd 0
100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax
eax
ebx
ecx
edx
eip
a dd 3b dd 5sum dd 0
100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax
eax
ebx
ecx
edx
eip
a dd 3b dd 5sum dd 0
100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax
eax
ebx
ecx
edx
eip
Tick!
D. Thiebaut, Computer Science, Smith College
Frequency: 3.2 GHz
cycle: 1/3.2 GHz =0.3125 ns
sec ms us ns
D. Thiebaut, Computer Science, Smith College
Arduino
;hello.asm; turns on an LED which is connected to PB5 (digital out 13)
.include "./m328Pdef.inc"
ldi r16,0b00100000out DDRB,r16out PortB,r16
Start:rjmp Start
Clock speed: 16 MHz
~1/200 speed of Pentium
http://www.science.smith.edu/dftwiki/index.php/Comparing_Different_Computers_with_N_Queens_Program
D. Thiebaut, Computer Science, Smith College
Raspberry Pi
/* -- first.s *//* This is a comment */.global main /* 'main' is our entry point and must be global */ main: /* This is main */ mov r0, #2 /* Put a 2 inside the register r0 */ bx lr /* Return from main */
Clock speed: 1.4 GHz
~1/3 speed of Pentium
http://www.science.smith.edu/dftwiki/index.php/Comparing_Different_Computers_with_N_Queens_Program
D. Thiebaut, Computer Science, Smith College
N-Queens Problem
http://www.science.smith.edu/dftwiki/index.php/Comparing_Different_Computers_with_N_Queens_Program
D. Thiebaut, Computer Science, Smith College
We Stopped Here Last Time…
D. Thiebaut, Computer Science, Smith College
5:30-6:30 p.m.
D. Thiebaut, Computer Science, Smith College
Evening Plans?
Let’s go to Boston this evening. I’ll drive!
D. Thiebaut, Computer Science, Smith College
Evening Plans?
Let’s go to Boston this evening. I’ll drive!
D. Thiebaut, Computer Science, Smith College
Evening Plans?
Let’s go to downtown Northampton.
D. Thiebaut, Computer Science, Smith College
Know what's in your toolbox!
D. Thiebaut, Computer Science, Smith College
Summit: Fastest SupercomputerOak Ridge National Laboratory
https://www.top500.org/lists/2019/06/
D. Thiebaut, Computer Science, Smith College
NUMBER SYSTEMSNUMBER SYSTEMS
D. Thiebaut, Computer Science, Smith College
Decimal
• Number of digits, the base
• Count in decimal
• Express number as sum of products
• Add two digits
• Add two numbers
D. Thiebaut, Computer Science, Smith College
Binary
• Number of digits, the base
• Count in binary
• Express number as sum of products
• Add two digits
• Add two numbers
D. Thiebaut, Computer Science, Smith College
Base 3
• Number of digits, the base
• Count in base 3
• Express number as sum of products
• Add two digits
• Add two numbers
D. Thiebaut, Computer Science, Smith College
Hexadecimal
• Number of digits, the base
• Count in hex
• Express number as sum of products
• Add two digits
• Add two numbers
D. Thiebaut, Computer Science, Smith College
Super
Useful!
Conversion0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15
D. Thiebaut, Computer Science, Smith College
Binary to Decimal,Decimal to
Binary ConversionReview
Shifting
D. Thiebaut, Computer Science, Smith College
101001 =
D. Thiebaut, Computer Science, Smith College
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
D. Thiebaut, Computer Science, Smith College
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
= 32 + 0 + 8 + 0 + 0 + 1 = 41d
D. Thiebaut, Computer Science, Smith College
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
= 32 + 0 + 8 + 0 + 0 + 1 = 41d
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
2 2
D. Thiebaut, Computer Science, Smith College
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
= 32 + 0 + 8 + 0 + 0 + 1 = 41d
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
2 2
= 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20 2 2 2 2 2 2
D. Thiebaut, Computer Science, Smith College
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
= 32 + 0 + 8 + 0 + 0 + 1 = 41d
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
2 2
= 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20 2 2 2 2 2 2 = 1x24 + 0x23 + 1x22 + 0x21 + 0x20
D. Thiebaut, Computer Science, Smith College
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
= 32 + 0 + 8 + 0 + 0 + 1 = 41d
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
2 2
= 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20 2 2 2 2 2 2 = 1x24 + 0x23 + 1x22 + 0x21 + 0x20 = 10100
D. Thiebaut, Computer Science, Smith College
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
= 32 + 0 + 8 + 0 + 0 + 1 = 41d
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
2 2
= 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20 2 2 2 2 2 2 = 1x24 + 0x23 + 1x22 + 0x21 + 0x20
= 10100 = 16 + 0 + 4 + 0 + 0 = 20d
D. Thiebaut, Computer Science, Smith College
Dividing by the baseextracts the leastsignificant digit
D. Thiebaut, Computer Science, Smith College
Python Program# prompts user for an integer# decomposes the integer into binary
x = int( input( "> " ) )binary = ""
while True: if x==0:
break
remainder = x % 2 quotient = x // 2
if remainder == 0: binary = "0" + binary else: binary = "1" + binary
print( "%5d = %5d * 2 + %d quotient=%5d remainder=%d binary=%16s" % (x, quotient, remainder, quotient, remainder, binary ) )
x = quotient
getcopy decimal2binary.py
D. Thiebaut, Computer Science, Smith College
Python Program
# prompts user for an integer# decomposes the integer into binary
x = int( input( "> " ) )binary = ""
while True: if x==0:
break
remainder = x % 2 quotient = x // 2
if remainder == 0: binary = "0" + binary else: binary = "1" + binary
print( "%5d = %5d * 2 + %d quotient=%5d remainder=%d binary=%16s" % (x, quotient, remainder, quotient, remainder, binary ) )
x = quotient
D. Thiebaut, Computer Science, Smith College
Binary to Decimal
D. Thiebaut, Computer Science, Smith College
101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20
= 32 + 0 + 8 + 0 + 0 + 1 = 41d
D. Thiebaut, Computer Science, Smith College
Binary to Hex
D. Thiebaut, Computer Science, Smith College
1010010 =
D. Thiebaut, Computer Science, Smith College
1010010 = 01010010
D. Thiebaut, Computer Science, Smith College
1010010 = 01010010 = 0101 0010
v
D. Thiebaut, Computer Science, Smith College
1010010 = 01010010 = 0101 0010
v
0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F
D. Thiebaut, Computer Science, Smith College
1010010 = 01010010 = 0101 0010 = 5 2
0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F
D. Thiebaut, Computer Science, Smith College
Hex to Binary
D. Thiebaut, Computer Science, Smith College
5A1F =
0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F
D. Thiebaut, Computer Science, Smith College
5A1F = 0101 1010 0001 1111
0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F
D. Thiebaut, Computer Science, Smith College
Hex to Decimal
D. Thiebaut, Computer Science, Smith College
1A2F =
D. Thiebaut, Computer Science, Smith College
1A2F = 1x163 + Ax162 + 2x161 + Fx160 =
D. Thiebaut, Computer Science, Smith College
1A2F = 1x163 + Ax162 + 2x161 + Fx160 = 1x4096 + 10x256 + 2x16 + 15x1 =
0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F
D. Thiebaut, Computer Science, Smith College
1A2F = 1x163 + Ax162 + 2x161 + Fx160 = 1x4096 + 10x256 + 2x16 + 15x1 = 4096 + 2560 + 32 + 15 =
0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F
D. Thiebaut, Computer Science, Smith College
1A2F = 1x163 + Ax162 + 2x161 + Fx160 = 1x4096 + 10x256 + 2x16 + 15x1 = 4096 + 2560 + 32 + 15 = 6703
0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 A 1011 B 1100 C 1101 D 1110 E 1111 F
D. Thiebaut, Computer Science, Smith College
Decimal toHex
D. Thiebaut, Computer Science, Smith College
Do:Decimal —> Binary
Binary—> Hex
D. Thiebaut, Computer Science, Smith College
Exercises
http://www.science.smith.edu/dftwiki/index.php/CSC231_Review_of_hexadecimal_number_system
D. Thiebaut, Computer Science, Smith College
Convert these hexadecimal numbers to binary, and to decimal.
1111
1234
AAAA
F001
FFFF
D. Thiebaut, Computer Science, Smith College
Convert these decimal numbers to binary, and hexadecimal
65
127
D. Thiebaut, Computer Science, Smith College
What comes after these hexadecimal numbers, logically?
aaAA
9999
19F
1ABF
FFEF
F00F
ABCDEF
D. Thiebaut, Computer Science, Smith College
Perform the following additions in hex
1000 + AAAA
1234 + F
1234 + ABCD
FFFF + FFFF
D. Thiebaut, Computer Science, Smith College
More ArithmeticInstructions
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
Same Computation in Python…