mith College Computer Science CSC231 - Assembly Week #8 Dominique Thiébaut

Similar documents
mith College Computer Science CSC231 Assembly Week #9 Fall 2017 Dominique Thiébaut

mith College Computer Science CSC231 Assembly Week #5 Fall 2017 Dominique Thiébaut

mith College Computer Science CSC231 - Assembly Week #4 Dominique Thiébaut

mith College Computer Science CSC231 - Assembly Week #3 Dominique Thiébaut

mith College Computer Science CSC231 - Assembly Week #9 Dominique Thiébaut

mith College Computer Science CSC231 Assembly Week #11 Fall 2017 Dominique Thiébaut

mith College Computer Science Fixed-Point & Floating-Point Number Formats CSC231 Dominique Thiébaut

mith College Computer Science Fixed & Floating Point Formats CSC231 Fall 2017 Week #15 Dominique Thiébaut

mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut

mith College Computer Science CSC231 Assembly Week #13 Fall 2017 Dominique Thiébaut

mith College Computer Science Fixed-Point & Floating-Point Number Formats CSC231 Assembly Language Week #13 Dominique Thiébaut

mith College Computer Science Fixed-Point & Floating-Point Number Formats CSC231 Assembly Language Week #14 Dominique Thiébaut

mith College Computer Science Week 14 Fixed & Floating Point Formats CSC231 Fall 2017 Week #14 Dominique Thiébaut

UMBC. A register, an immediate or a memory address holding the values on. Stores a symbolic name for the memory location that it represents.

mith College Computer Science CSC231-Assembly Fall 2017 Week #2 Dominique Thiébaut

mith College Computer Science CSC231 Assembly Week #10 Fall 2017 Dominique Thiébaut

Name: CMSC 313 Fall 2001 Computer Organization & Assembly Language Programming Exam 1. Question Points I. /34 II. /30 III.

Groups of two-state devices are used to represent data in a computer. In general, we say the states are either: high/low, on/off, 1/0,...

Introduction to 8086 Assembly

ADDRESSING MODES. Operands specify the data to be used by an instruction

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Faculty of Engineering Computer Engineering Department Islamic University of Gaza Assembly Language Lab # 2 Assembly Language Fundamentals

mith College Computer Science CSC231 Assembly Week #12 Thanksgiving 2017 Dominique Thiébaut

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

LAB 5 Arithmetic Operations Simple Calculator

Representation of Information

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

CONCORDIA UNIVERSITY Department of Computer Science and Software Engineering COMP 228/4 Section PP Midterm Exam

CS 3843 Final Exam Fall 2012

CSC 8400: Computer Systems. Machine-Level Representation of Programs

Beginning C Programming for Engineers

MODE (mod) FIELD CODES. mod MEMORY MODE: 8-BIT DISPLACEMENT MEMORY MODE: 16- OR 32- BIT DISPLACEMENT REGISTER MODE

Basic Assembly Instructions

Binary Logic (review)

M1 Computers and Data

By: Dalbir Singh, Computer Science Dep't

reply db y prompt db Enter your favourite colour:, 0 colour db 80 dup(?) i db 20 k db? num dw 4000 large dd 50000

SIGNED AND UNSIGNED SYSTEMS

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Lab 4: Basic Instructions and Addressing Modes

Adding Binary Integers. Part 4. Negative Binary. Integers. Adding Base 10 Numbers. Adding Binary Example = 10. Arithmetic Logic Unit

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

Defining and Using Simple Data Types

Basic Pentium Instructions. October 18

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Assembly basics CS 2XA3. Term I, 2017/18

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

CS 241 Data Organization Binary

Computer Architecture and Assembly Language. Practical Session 5

Lab 3. The Art of Assembly Language (II)

CMSC 313 Lecture 08 Project 2 Questions Recap Indexed Addressing Examples Some i386 string instructions A Bigger Example: Escape Sequence Project

Basic Assembly SYSC-3006

Faculty of Engineering Student Number:

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

16.317: Microprocessor Systems Design I Fall 2015

Topic Notes: Bits and Bytes and Numbers

Practical Malware Analysis

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

data within a computer system are stored in one of 2 physical states (hence the use of binary digits)

Lab 6: Conditional Processing

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

Giving credit where credit is due

More Programming Constructs -- Introduction

Topic Notes: Bits and Bytes and Numbers

Semester Transition Point. EE 109 Unit 11 Binary Arithmetic. Binary Arithmetic ARITHMETIC

X86-NASM STANDARD COMMANDS. Comment your code with a semicolon (;)! The assembler won t read anything after it.

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

CSC231 C Tutorial Fall 2018 Introduction to C

Computer Organization & Systems Exam I Example Questions

CPU. IBM PC and compatible Memory Structure

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

Microprocessor and Assembly Language Week-5. System Programming, BCS 6th, IBMS (2017)

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Chapter 3: part 3 Binary Subtraction

CSCI 2121 Computer Organization and Assembly Language PRACTICE QUESTION BANK

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Binghamton University. CS-220 Spring x86 Assembler. Computer Systems: Sections

Topics of this Slideset. CS429: Computer Organization and Architecture. It s Bits All the Way Down. Why Binary? Why Not Decimal?

CS429: Computer Organization and Architecture

Introduction to 8086 Assembly

Chapter 4 Processor Architecture: Y86 (Sections 4.1 & 4.3) with material from Dr. Bin Ren, College of William & Mary

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

System Programming and Computer Architecture (Fall 2009)

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

SECONDARY SCHOOL, L-IMRIEĦEL HALF YEARLY EXAMINATIONS 2016/2017

CPSC 213. Introduction to Computer Systems. Reading. Control Flow. Loops (S5-loop) Static Control Flow. Unit 1d. Companion.

Computer Components. Software{ User Programs. Operating System. Hardware

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

The Instruction Set. Chapter 5

Basic Arithmetic (adding and subtracting)

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

Transcription:

mith College Computer Science CSC231 - Assembly Week #8 Dominique Thiébaut dthiebaut@smith.edu

Can't Sleep https://xkcd.com/571/

Logic Design

Logic Design

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

Nasa Computer (Apollo)

A Bit of History Claude Shannon 21 years old 1937 MIT Master's Thesis All arithmetic operations on binary numbers can be performed using boolean logic operators https://en.wikipedia.org/wiki/claude_shannon

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

Designing a 2-bit Adder with Logic

Processor CU ALU

xi yi ALU Compute add x, y carryi sumi

x y xn yn x3 y3 x2 y2 x1 y1 x0 y0 zn z3 z2 z1 z0 z

y0 x0 0 0 1 1 + 0 + 1 + 0 + 1 = 0 0 = 0 1 = 0 1 = 1 0 carry z0

x0 y0 Carry z0 0 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0

x0 y0 Carry z0 0 0 0 0 0 1 0 1 1 0 0 1 Carry = x0 and y0 z0 = x0 xor y0 1 1 1 0

x0 y0 AND XOR carry z0

Moral of the Story: Addition is performed by logic operations using natural binary numbers (unsigned arithmetic)

NEGATIVE NUMBERS

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

4-bit Nybble Binary Hex Unsigned Decimal 0000 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

Sign Bit 4-bit Nybble Binary Hex Unsigned Decimal 0 000 0 0 0 001 1 1 0 010 2 2 0 011 3 3 0 100 4 4 0 101 5 5 0 110 6 6 0 111 7 7 1 000 8 8 1 001 9 9 1 010 A 10 1 011 B 11 1 100 C 12 1 101 D 13 1 110 E 14 1 111 F 15

4-bit Nybble Binary Hex Unsigned Decimal 0 000 0 0 0 001 1 1 0 010 2 2 0 011 3 3 0 100 4 4 0 101 5 5 0 110 6 6 0 111 7 7 1 000 8 8 1 001 9 9 1 010 A 10 1 011 B 11 1 100 C 12 1 101 D 13 1 110 E 14 1 111 F 15 Positive Numbers Negative Numbers

Signed Magnitude Number System

Signed Magnitude Rule: To find the opposite of a number, just flip its MSB 0 101 (+5) 1 101 (-5) and vice versa

4-bit Nybble Binary Hex Unsigned Decimal Signed Magnitude 0 000 0 0 +0 0 001 1 1 +1 0 010 2 2 +2 0 011 3 3 +3 0 100 4 4 +4 0 101 5 5 +5 0 110 6 6 +6 0 111 7 7 +7 1 000 8 8-0 1 001 9 9-1 1 010 A 10-2 1 011 B 11-3 1 100 C 12-4 1 101 D 13-5 1 110 E 14-6 1 111 F 15-7

Does this System work With the ALU Adder? Binary Hex Unsigned Decimal Signed Magnitud e 0 000 0 0 +0 0 001 1 1 +1 0 010 2 2 +2 0 011 3 3 +3 0 100 4 4 +4 0 101 5 5 +5 0 110 6 6 +6 0 111 7 7 +7 1 000 8 8-0 1 001 9 9-1 1 010 A 10-2 1 011 B 11-3 1 100 C 12-4 1 101 D 13-5 1 110 E 14-6 1 111 F 15-7 3 + -3 - = 0 4 + -1 - = 3

1's Complement Number System

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

4-bit Nybble Binary Hex Unsigned Decimal 1's Complement 0 000 0 0 +0 0 001 1 1 +1 0 010 2 2 +2 0 011 3 3 +3 0 100 4 4 +4 0 101 5 5 +5 0 110 6 6 +6 0 111 7 7 +7 1 000 8 8-7 1 001 9 9-6 1 010 A 10-5 1 011 B 11-4 1 100 C 12-3 1 101 D 13-2 1 110 E 14-1 1 111 F 15-0

Does this System work With the ALU Adder? Binary Hex Unsigned Decimal 1's Complement 0 000 0 0 +0 0 001 1 1 +1 0 010 2 2 +2 0 011 3 3 +3 0 100 4 4 +4 0 101 5 5 +5 0 110 6 6 +6 0 111 7 7 +7 1 000 8 8-7 1 001 9 9-6 1 010 A 10-5 1 011 B 11-4 1 100 C 12-3 1 101 D 13-2 1 110 E 14-1 1 111 F 15-0 3 + -3 - = 0 5 + -3 - = 2 4 + -1 - = 3

2's Complement Number System

2's Complement Rule: To find the opposite of a number, just flip all its bits, and add 1 1 010 + 1 0 101 (+5) 1 011 (-5) 0 100 + 1 and vice versa

4-bit Nybble Binary Hex Unsigned Decimal 2's Complement 0 000 0 0 +0 0 001 1 1 +1 0 010 2 2 +2 0 011 3 3 +3 0 100 4 4 +4 0 101 5 5 +5 0 110 6 6 +6 0 111 7 7 +7 1 000 8 8-8 1 001 9 9-7 1 010 A 10-6 1 011 B 11-5 1 100 C 12-4 1 101 D 13-3 1 110 E 14-2 1 111 F 15-1

Does this System work With the ALU Adder? Binary Hex Unsigned Decimal 2's Complement 0 000 0 0 +0 0 001 1 1 +1 0 010 2 2 +2 0 011 3 3 +3 0 100 4 4 +4 0 101 5 5 +5 0 110 6 6 +6 0 111 7 7 +7 1 000 8 8-8 1 001 9 9-7 1 010 A 10-6 1 011 B 11-5 1 100 C 12-4 1 101 D 13-3 1 110 E 14-3 1 111 F 15-1 3 + -3 - = 0 5 + -3 - = 2 4 + -1 - = 3

Interesting Property 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?

int x = 0x7fffffff - 5; for ( int i=0; i<10; i++ ) System.out.println( x++ ); getcopy Loop0x7fffffff.java

What did you just learn about Java ints?

neg neg oprnd neg op8 neg op16 neg op32 op: mem, reg alpha db 1 beta dw 4 x dd 0xF06 neg byte[alpha] mov ax,1234 neg ax To get the 2's complement of an int neg dword[x]

Range of 2's Comp't. ints Binary Hex Unsigned Decimal 0000 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

Type Minimum Maximum unsigned byte signed byte unsigned short signed short unsigned int signed int # Bytes 0 255 1-128 127 1 0 65,535 2-32,768 32,767 2 0 4,294,967,295 4-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

We Stopped Here Last Time (and finished first exercise on next slide) http://www.genengnews.com/media/images/genhighlight/mar12_2014_8296129_stopsigns_gerontrialhalt2322361121.jpg

https://www.youtube.com/ watch?v=dtydzkpmfou The 13th Floor

The LOOP Instruction

loop loop label loop label x dd 1 sum dd 0 mov ecx, 10 addup: mov eax, dword[x] add dword[sum], eax inc dword[x] loop addup ;ecx< ecx-1 ;if ecx!=0, ; goto addup

loop loop label loop label x dd 1 sum dd 0 Label mov ecx, 10 addup: mov eax, dword[x] add dword[sum], eax inc dword[x] loop addup ;ecx< ecx-1 ;if ecx!=0, ; goto addup

loop loop label loop label ;ecx < ecx-1 ;if ecx!= 0, ; goto label ;else continue x dd 1 sum dd 0 Label mov ecx, 10 addup: mov eax, dword[x] add dword[sum], eax inc dword[x] loop addup ;ecx< ecx-1 ;if ecx!=0, ; goto addup

_start: mov eax, 4 Labels for1: for2: mov ecx, 10 loop for1 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

Tracing a Code Example

eax ecx mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1 ;if ecx!=0, ; goto for

eax ecx? 3 mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1 ;if ecx!=0, ; goto for

eax ecx 1 3 mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1 ;if ecx!=0, ; goto for

1 eax ecx 1 3 mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1 ;if ecx!=0, ; goto for

1 eax ecx 1 2 3 mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1 ;if ecx!=0, ; goto for

1 eax ecx 1 2 3 2 mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1 ;if ecx!=0, ; goto for

12 eax ecx 1 2 3 2 mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1 ;if ecx!=0, ; goto for

12 eax ecx 1 2 3 3 2 mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1 ;if ecx!=0, ; goto for

12 eax ecx 1 2 3 3 2 1 mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1 ;if ecx!=0, ; goto for

123 eax ecx 1 2 3 3 2 1 mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1 ;if ecx!=0, ; goto for

123 eax ecx 1 2 3 4 3 2 1 mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1 ;if ecx!=0, ; goto for

123 eax ecx 1 2 3 4 3 2 1 0 mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1 ;if ecx!=0, ; goto for

123 eax ecx 1 2 3 4 3 2 1 0 mov ecx, 3 mov eax, 1 for: call _printdec inc eax loop for ;ecx< ecx-1???? ;if ecx!=0, ; goto for

Example 1 Sum of 1..10

; computes sum(1,2, 10) x dd 1 sum 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

; computes sum(1,2, 10) x dd 1 sum dd 0 Do we need eax? mov ecx, 10 addup: add dword[sum], ecx loop addup ;ecx< ecx-1 ;if ecx!=0, ; goto addup mov eax, dword[sum] call _printdec

Example 2 Fibonaccis http://i.dailymail.co.uk/i/pix/2016/07/17/13/1a32203e000005dc-3694326-image-m-17_1468760305397.jpg

; print the first 10 Fibonacci terms _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

Looping Through Arrays

LOOP INSTRUCTION Looping Through Arrays INDIRECT ADDRESSING MODE

Indirect Addressing Mode The addressing mode refers to the way the operand of an instruction is generated. We already know register mode, immediate mode, and direct mode.

Tracing One Example of Indirect Addressing (Base Addressing)

ebx??? 0x1104C 0x1104B Memory 12 33 al section.data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12? 0x1104A 0x11049 0x11048 78 56 3E section.text _start: mov al, 'z' mov ebx, A mov byte[ebx], 0 0x11047 0x11046 F0 3 mov ebx, B mov byte[ebx], al 0x11045 1

ebx??? 0x1104C 0x1104B Memory 12 33 al section.data A db 1,3,0xF0,0x3E,0x56 B db 0x78,0x33,0x12 'z' 0x1104A 0x11049 0x11048 78 56 3E section.text _start: mov al, 'z' mov ebx, A mov byte[ebx], 0 0x11047 0x11046 F0 3 mov ebx, B mov byte[ebx], al 0x11045 1

0x1104C 0x1104B 0x1104A 0x11049 0x11048 0x11047 0x11046 0x11045 Memory 12 33 78 56 3E F0 3 1 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 11045 'z'

0x1104C 0x1104B 0x1104A 0x11049 0x11048 0x11047 0x11046 0x11045 Memory 12 33 78 56 3E F0 3 1 0 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 11045 'z'

0x1104C 0x1104B 0x1104A 0x11049 0x11048 0x11047 0x11046 0x11045 Memory 12 33 78 56 3E F0 3 1 0 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 1104A 'z'

0x1104C 0x1104B 0x1104A 0x11049 0x11048 0x11047 0x11046 0x11045 Memory 12 33 78 'z' 56 3E F0 3 1 0 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 1104A 'z'

Example 2: Setting an Array to All 0s

; Array Table contains 10 words Table dw 1,2,3,4,5,6 dw 7,8,9,10 mov ecx, ;# of elements mov ebx, ;address of ;Table clear: mov word[ebx], ;value to store add ebx, ;make ebx point ;to next word loop clear ;ecx< ecx-1 ;if ecx!=0, ; goto clear

Exercises Problem #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.

We stopped here last time