csc231 - assembly · 2019. 10. 9. · d. thiebaut, computer science, smith college a dd 3 b dd 5...

83
mith College Computer Science Dominique Thiébaut [email protected] CSC231 - Assembly Week #5 Fall 2019

Upload: others

Post on 29-Mar-2021

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

mith College

Computer Science

Dominique Thiébaut [email protected]

CSC231 - AssemblyWeek #5 Fall 2019

Page 2: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 3: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Misc. Notes & Review

Page 4: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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)

Page 5: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 6: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Review: Our Goal was…

int x, y, sum;

x = 3;y = 5;sum = x + y;

Page 7: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 8: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Same with Bytes

byte x, y, sum;

x = 3;y = 5;sum = x + y;

Translate this into Assembly

Page 9: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 10: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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?

Page 11: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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?

Page 12: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 13: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Follow a stepby step execution

of the program

Page 14: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 15: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 16: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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!

Page 17: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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!

Page 18: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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!

Page 19: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Frequency: 3.2 GHz

cycle: 1/3.2 GHz =0.3125 ns

sec ms us ns

Page 20: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 21: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 22: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

N-Queens Problem

http://www.science.smith.edu/dftwiki/index.php/Comparing_Different_Computers_with_N_Queens_Program

Page 23: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

We Stopped Here Last Time…

Page 24: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

5:30-6:30 p.m.

Page 25: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Evening Plans?

Let’s go to Boston this evening. I’ll drive!

Page 26: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Evening Plans?

Let’s go to Boston this evening. I’ll drive!

Page 27: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Evening Plans?

Let’s go to downtown Northampton.

Page 28: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Know what's in your toolbox!

Page 29: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Summit: Fastest SupercomputerOak Ridge National Laboratory

https://www.top500.org/lists/2019/06/

Page 30: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

NUMBER SYSTEMSNUMBER SYSTEMS

Page 31: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 32: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 33: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 34: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 35: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 36: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Binary to Decimal,Decimal to

Binary ConversionReview

Shifting

Page 37: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

101001 =

Page 38: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20

Page 39: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20

= 32 + 0 + 8 + 0 + 0 + 1 = 41d

Page 40: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 41: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 42: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 43: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 44: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 45: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Dividing by the baseextracts the leastsignificant digit

Page 46: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 47: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 48: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Binary to Decimal

Page 49: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

101001 = 1x25 + 0x24 + 1x23 + 0x22 + 0x21 + 1x20

= 32 + 0 + 8 + 0 + 0 + 1 = 41d

Page 50: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Binary to Hex

Page 51: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

1010010 =

Page 52: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

1010010 = 01010010

Page 53: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

1010010 = 01010010 = 0101 0010

v

Page 54: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 55: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 56: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Hex to Binary

Page 57: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 58: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 59: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Hex to Decimal

Page 60: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

1A2F =

Page 61: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

1A2F = 1x163 + Ax162 + 2x161 + Fx160 =

Page 62: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 63: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 64: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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

Page 65: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Decimal toHex

Page 66: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Do:Decimal —> Binary

Binary—> Hex

Page 67: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Exercises

http://www.science.smith.edu/dftwiki/index.php/CSC231_Review_of_hexadecimal_number_system

Page 68: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Convert these hexadecimal numbers to binary, and to decimal.

1111

1234

AAAA

F001

FFFF

Page 69: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Convert these decimal numbers to binary, and hexadecimal

65

127

Page 70: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

What comes after these hexadecimal numbers, logically?

aaAA

9999

19F

1ABF

FFEF

F00F

ABCDEF

Page 71: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Perform the following additions in hex

1000 + AAAA

1234 + F

1234 + ABCD

FFFF + FFFF

Page 72: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

More ArithmeticInstructions

Page 73: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

ALUALU

Page 74: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

Right now, we are dealing only

with UNSIGNED integers!

Page 75: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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 76: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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 77: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

mulmul reg8mul reg16mul reg32mul mem8mul mem16mul mem32

mul operand

Page 78: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Observation

1001x 1110

Page 79: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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 80: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

This has tremendously important consequences!

Page 81: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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 82: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

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 83: CSC231 - Assembly · 2019. 10. 9. · D. Thiebaut, Computer Science, Smith College a dd 3 b dd 5 sum dd 0 100 mov eax, dword[a] 105 add eax, dword[b] 10A sub eax, 1 10E mov dword[sum],

D. Thiebaut, Computer Science, Smith College

Same Computation in Python…