IBM PC Hardware CPU 8088, 8086 80286 80386 80486 Pentium... ALU (Arithmetic and Logic Unit) Registers CU (Control Unit) IP Memory ROM BIOS I/O RAM OS Programs Video memory BIOS data Interrupt Vectors Memory Address The Fetch-Execute Cycle While True Fetch instruction Update IP Execute fetched instruction Assembly Language Computers speak machine language (in binary code) CPU MEMORY Register AX x a In C++ x += a; Compiler translates C++ program to.obj file (binary code) In machine (binary) code Move x to ax In Assembly mov ax, x (1010 0001 0000 0000 0010 1110) Page 1 of 18
Add a to ax (0000 0011 0000 0110 0000 0000 0011 0000) Move ax to x add ax, a mov x, ax (1010 0011 0000 0000 0010 1110) For each machine instruction, there is an assembly statement Machine language is rarely used Assembly language is used sometimes Assembler translates assembly program to machine code To understand To improve performance CISC vs RISC Instruction Set All a computer can do CISC RISC Complex Instruction Set Computing Reduced Instruction Set Computing All IBM PCs use CISC Lecture 2 DOS Different from Windows Application under Windows Commands Help /? Change Drive A: Change directory Cd.. cd masm615 List all files/directories dir Edit files Use PWB Path = c:\masm615\bin Up-Down arrows Display the last or previous command Page 2 of 18
Steps in Assembly Programming a. Create source program (First.asm) using any text editor b. MASM First (to produce object file First.obj) c. LINK First (to produce executable file First.exe) d. First to run the program) e. CV First (to debug the program) In CV run program step over break point watch registers and others look at user screen [Alt]+F5 cannot edit Basic Concepts MASM also generates other files: First.lst, First.map Assembly is case insensitive Assembly Program has three segments Stack, data, and code Pseudo Operations Not machine instructions Give information to Assembler.MODEL SMALL.STACK 256.DATA.CODE ;specify the structure of segments ;small model is enough in our class ;stack size: 100H ;declare variables or constants ;instructions Data segment Labels: symbolic names Key Words: Name, Title, Page,... Pseudo operations: DB and DW Define variables with or without initial values Data types: byte and word Registers AX, BX, CX, DX, DS Each one is a word, and has a high byte and a low byte AX: AH AL Standard Boilerplate mov ax, @Data ;Cannot use: mov ds, @Data mov ds, ax Macros and Sub-procedures Like high-level languages, Assembly provides macros and subprocedures The difference between macros and sub-procedures Macro: Large instruction Page 3 of 18
The assembler expands macros and we get the same.obj file as without macros. We ask the assembler to write code for us, and use macros to tell assembler what to write Macros Sub-procedures are like C++ functions Function calls transfer control IP does not go to the next instruction It is set to the address of the function called Save registers Execute slowly More manageably at a high level pcmac.inc _PutStr label ;Macro to display a string Be Careful! These macros will use the registers The values stored in the registers will be destroyed. Mov ax, 1024 _PutStr MyName ;what is in ax? Mov ax, 1024 ;ax: 0000 0100 0000 0000 Mov dx, OFFSET MyName Mov ah, 9h ;ax: 0000 1001 0000 0000 Int 21h sputstr label ;Safe version ;Use this one! sputch '$', 13, 10 ;Macro to display a chars ;up to 10 chars Equivalent code mov al, '$' mov ah, 2h int 21h sexit 0 ;Macro to exit with 0 sgetdate ;after the call ;al = weekday (0-6) ;dh = month (1-12) ;dl = day (1-31) ;cx = year (4 digits) The assembly program begins with the line INCLUDE PCMAC.INC.MODEL SMALL ;before.model SMALL Sub-Procedures util.lib PutDec PutHex GetDec Page 4 of 18
PutDec Display a number (word) The number is in ax mov ax, 1024 call PutDec To use the sub-procedure: mov ax, cx ;display number in cx call PutDec mov ah, 0 ;display number in al call PutDec Add the line at the beginning of code segment.code EXTRN PutDec : NEAR EXTRN GetDec : NEAR EXTRN PutDec : NEAR, PutHex : NEAR, GetDec : NEAR PutHex GetDec Display hex number (signed or unsigned) The number is in ax Input a signed number The number is returned in ax No prompt Files Created by Assembler Lab1.lst List program with machine code Lab1.map Memory map Instruction MOV MOV dest, source Copy source to dest From right to left Same as in C++: x = y; Must be the same type Binary Numbers Decimal Numbers Binary Numbers Base 10 with ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 323 = 300 + 20 + 3 = 3 * 100 + 2 * 10 + 3 * 1 = 3 * (10 **2) + 2 * (10 **1) + 3 * (10 ** 0) Computers use binary numbers: base 2 Bit: Two stable states Two digits: 0 & 1 Page 5 of 18
IBM PCs Byte: Word: Double Word: 8 Bits 2 bytes (16 Bits) 4 Bytes (32 Bits) Binary to Decimal Conversion 1 0 0 1 0 0 1 1 1 * (2**7) + 1 * (2**4) + 1 * (2**1) + 1 * (2**0) = 128 + 16 + 2 + 1 = 147 Decimal to Binary Division (divisor: 2) Quotient Remainder 147 73 1 36 1 18 0 9 0 4 1 2 0 1 0 0 1 The first remainder is the lowest bit: 10010011B Other ways Addition in Binary Binary numbers 1B + 1B = 10B carry 1 1 1 1 1 1 1 1 1 0 1 0 1 1 B (235D) + 1 0 1 0 0 1 1 0 B (166D) --------------------------------------- 1 1 0 0 1 0 0 0 1 B (401D) Overflow on one byte Flag register and Overflow flag (bit) Subtraction in Binary borrows 1 1 1 1 0 1 0 1 1 B (235) - 1 0 1 0 0 1 1 0 B (166) ------------------------------- 0 1 0 0 0 1 0 1 B ( 69) borrows 1 1 1 1 1 1 1 1 0 1 0 0 1 1 0 B (166) - 1 1 1 0 1 0 1 1 B (235) -------------------------------- 1 0 1 1 1 0 1 1 B (-69) Wrong! Should be a negative number! Page 6 of 18
Signed Numbers How to represent negative numbers in Binary system? Sign-Magnitude Representation One bit for Sign: the left most digit Two different bytes for one number: zero 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1's Complement Representation -x = 1 1 1 1 1 1 1 1 B - x Reverse every bit Still two different bytes for one number: zero 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2's Complement Representation IBM PC and most modern computers 2 s Complement Signed Binary Integers For any number x (one byte) -x = 1 1 1 1 1 1 1 1 B - x + 1 = (1 1 1 1 1 1 1 1 B + 1) - x = 2**8 - x 2's Complement Representation: IBM PC and most modern computers x : signed byte -x = 1 1 1 1 1 1 1 1 B - x + 1 = (1 1 1 1 1 1 1 1 B + 1) - x = 2**8 - x 51: 0 0 1 1 0 0 1 1 B 2's Complement operation on 51 1 1 1 1 1 1 1 1 B - 0 0 1 1 0 0 1 1 B --------------------------------- 1 1 0 0 1 1 0 0 B + 0 0 0 0 0 0 0 1 B --------------------------------- 1 1 0 0 1 1 0 1 B 2's complement representation on -51: 2's complement operation Apply to x ==> -x Apply to -x ==> -(-x) = x -(-51) = 51 x : signed word -x = 2**16 - x 1 1 0 0 1 1 0 1 B Page 7 of 18
Quick 2's Complement Keep all bits from right upto the first 1 Reverse all remaining bits 51 0 0 1 1 0 0 1 1 B -51 1 1 0 0 1 1 0 1 B -(-51) 0 0 1 1 0 0 1 1 B Sign and Magnitude 74 0 1 0 0 1 0 1 0 B -74 1 0 1 1 0 1 1 0 B -(-74) 0 1 0 0 1 0 1 0 B Use 2's complement representation Positive numbers: Left-most bit is 0 Negative numbers: Left-most bit is 1 Magnitude (absolute value) of 2's Complement 51 = 0 0 1 1 0 0 1 1 B Count ONEs Same as Unsigned Integers -51 = 1 1 0 0 1 1 0 1 B Count ZEROs and plus one Operations using 2's Complement Signed and Unsigned are treated the same way 51 0 0 1 1 0 0 1 1 B -51 + 1 1 0 0 1 1 0 1 B ----------------------------------- 1 0 0 0 0 0 0 0 0 B 51 + (-51) = 0 when the carry is discarded! Ex. 74 + (-51) = 23 0 1 0 0 1 0 1 0 B + 1 1 0 0 1 1 0 1 B --------------------------------- 0 0 0 1 0 1 1 1 B 0 1 0 0 1 0 1 0 B - 0 0 1 1 0 0 1 1 B ----------------------------------- 0 0 0 1 0 1 1 1 B Overflow Signed Unsigned Left most bit Page 8 of 18
Value Ranges Byte Unsigned: 0 to 255 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 Signed : -128 to 127 1 0 0 0 0 0 0 0-128 1 1 1 1 1 1 1 1-1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 1 1 1 1 127 Word Unsigned: 0 to 65535 Signed : -32768 to 32767 Signed Byte and Word Convert Byte to Word 0 0 1 1 0 0 1 1 B = 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 B 1 1 0 0 1 1 0 1 B = 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 1 B Convert Word to Byte 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 B = 0 0 1 1 0 0 1 1 B 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 1 B = 1 1 0 0 1 1 0 1 B How to do it? 1 1 0 1 1 1 1 1 1 1 0 0 1 1 0 1 B HEXADECIMAL NUMBERS Hexadecimal Integers (Hex) Shorthand representation for binary numbers Binary numbers are too long to write 1000 (D) = 1111101000B Group 4 binary digits together into one Hex digit Used in Assembly Language (not in computer) Binary Decimal Hex 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 10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F Page 9 of 18
Binary to Hex Group from right 0001 1001 1100 1011B = 19CBH 1110 1011 0101 0010B = 0EB52H Hex to Binary 8EH 19CBH = 1000 1110B = 0001 1001 1100 1011B Hex Numbers and Labels What about EACH? Hex number or Identifier? Numbers always begin with (decimal) digits! EACH: Identifier (Identifier never begins with digits) 0EACH: Hex Addition and Subtraction carry 1 1 0 A B C D H + 1 2 3 4 H -------------------- B E 0 1 H borrow 1 1 1 1 1 2 3 4 H - 0 A B C D H -------------------- 6 6 6 7 H What are the values in decimal? Hex to Decimal To Binary first Unsigned 8EH = 8 * (16 ** 1) + 14 * (16 ** 0) = 128 + 14 = 142 ** 0) 19CBH = 1 * (16 ** 3) + 9 * (16**2) + 12 * (16 **1) + 11 * (16 = 4096 + 9 * 256 + 12 * 16 + 11 = 6603 Signed 2's Complement -x = (1111 1111 B) - x + 1 = 0FFH - x + 1 8EH = 72H 19CBH = 0E635H Positive or Negative? 19CBH 79CBH 89CBH 0FBCDH Page 10 of 18
Decimal to Hex Binary first Unsigned Similar to Decimal to Binary: Division by 16 Quotient Remainder 6603 412 11 (B) 25 12 (C) 1 9 0 1 First remainder is the lowest digit: 19CBH Signed 2's Complement -6603 19CBH 0E635H Multiplication and Division Much higher cost Decimal Binary Hex Addition and Subtraction Instructions add dest, source ;dest := dest + source sub dest, source ;dest := dest - source Similar to mov same size at least one operand is not in memory EX A := B - C + 3 (All byte) A DB? B DB 87 C DB -42 mov al, B sub al, C add al, 3 mov A, al The usual way: move to register do it move back to memory Register ax: Accumulator Could use AH or any other byte register EX A := A + B (All word) Page 11 of 18
A DW? B DW 87 C DW -15842 add A, B ;Wrong! mov add mov ax, A ax, B A, ax Can we use only two instructions? More Instructions: dec A ;A := A - 1 inc ax ;ax := ax + 1 neg B ;B := -B ;2's complement Possible Overflow EX A := B - C + 3 (All byte, Signed) A DB? B DB 87 C DB -42 Mov AL, B Sub AL, C Overflow? Convert Byte to Word 87 0 1 0 1 0 1 1 1 57 H - (-42) - 1 1 0 1 0 1 1 0 - D6 H ------- -------------------- ---------- 129 1 0 0 0 0 0 0 1 81 H Unsiged Mov AH, 0 Sub AH, AH Signed AH AL???????? 1 0 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0???????? 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 Instruction Cbw ; Convert AL to AX ; AX has the same value as AL ; as signed integer Reverse conversion Page 12 of 18
Example EX A := B - C + 3 (B and C are byte) (A is word) (All signed) 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 (The value of AX is the same as AL) 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 1 1 1 1 1 1 0 0 0 0 0 0 0 (The value of AX is NOT the same as AL) NO! A DW? B DB 127 C DB -128 mov al, C cbw mov bx, ax ; C in BX mov cbw al, B ; B in AX sub ax, bx add ax, 3 mov A, ax As unsigned numbers A := B - C + 3 Multiplication Unsigned Mul Multiplier Signed imul Multiplier AX = AL * Multiplier (DX)(AX) = AX * Multiplier Operand Size Multiplicand Multiplier Product Byte al reg/mem ax Word ax reg/mem (dx)(ax) Range of the product No overflow 20 * 100 ----> 2000 byte * byte ----> word word * word ----> double word Page 13 of 18
Examples Ex 1 W := A * B both A and B are signed bytes W should be word A DB? B DB? W DW? Call Mov Call Mov GetDec A, AL GetDec B, AL mov al, A ;al := A imul B ;al is assumed ;ax := al * B mov W, ax Ex 2 product product W := A * B both A and B are unsigned words W must be double word mov ax, A ;ax := A mul B ;ax is assumed mov W1, ax ;ax is the low word of mov W2, dx ;dx is the high word of That is or (dx)(ax) := ax * B (W2)(W1) := A * B and W = (W2)(W1) Ex 3 W := A * (-15) mov al, A ;al := A imul -15 ;NO ;cannot use a constant * bl mov al, A mov bl, -15 mul bl ;al is assumed, and ax := al mov W, ax Division Instructions unsigned numbers div divisor signed numbers idiv divisor Integer Division Quotient Remainder Page 14 of 18
W B Q R Signed -84 5-16 -4 Unsigned 172 5 34 2 Multiplication W := A * B word byte byte Division A := W / B byte word byte Operand Size Dividend Divisor Quotient Remainder Byte ax reg/mem (byte) al ah Word (dx)(ax) reg/mem (word) ax dx Examples Q := W / B R := W % B a) W: word, others: byte, signed mov ax, W idiv B mov Q, al mov R, ah b) all are byte, signed mov al, W cbw idiv B mov Q, al mov R, ah ;convert al to ax Overflow c) all are byte, unsigned mov al, W mov ah, 0 ;convert (extend) al to ax ;or sub ah, ah div B mov Q, al mov R, ah Multiplication: NO Division : Possible! EX. 10,000 / 2 ----> 5,000 word byte more than a byte! COMPARING and BRANCHING Comparison Instruction cmp reg/mem, reg/mem/const Flags Register Same type: byte or word Subtract the numbers Set flags (Values are not modified) Page 15 of 18
16 bits Z: zero flag O: overflow flag C: carry flag S: sign flag Bit Number b15... b11.. b8 b7 b6... b1 b0 O S C Z Other operations may also change Flag Register Jumping Conditional Jump Signed Numbers jg, jl, jge, jle Unsigned Numbers ja jump if above jae jump if above or equal jb jump if below jnb jump if NOT below Both Signed and Unsigned je, jne jz, jnz Unconditional Jump jmp Example if ax <> 0 then Q = X / Y R = X % Y else Display Message //Next We have two blocks of statements Determine the Entrance and Exit for each block X DW? Y DB? Q DB? R DB? ;Input Signed numbers X and Y mov ax, X cmp ax, 0 ;compare ax and 0 je IsZero ;jump to MinusOne if ax >= bx idiv mov mov jmp Y Q, AL R, AH Next IsZero: sputstr Zero Next: ;instruction after the IF Page 16 of 18
IF Statements Jumping All values are signed If AX < BX X = 1 Else X = -1 Val ++ cmp AX, BX jnl Minus Mov X, 1 Jmp Next Minus: Next: Mov X, -1 Inc Val Examples Jump instructions modifies IP (The Fetch-and-Execute Loop) Displacement: Lable will be replaced with numbers Jump only when necessary! EX 1 All variables are signed word (Short Circuit Evaluation) if (A < B) and (A >= 0) then X := A else X := B X := X + 1 // next statement mov ax, A cmp ax, B ;compare A and B jge XisB ;jump to XisB if A >= B cmp ax, 0 jl XisB ;jump if A < 0 mov X, A ;X := A jmp Next XisB: Next: mov X, B ;X := B ;instruction after the IF inc X ;X := X + 1 EX 2 All variables are Unsigned byte if (A < B) or (A >= 10) then Page 17 of 18
X := A elseif A = B then X := 0 else X := B X := X + 1 // next statement mov al, A mov bl, B cmp al, bl jb XisA cmp al, 10 jb SecondIF XisA: mov jmp X, al Next SecondIf: cmp al, bl jne XisB mov X, 0 jmp Next XisB: Next: mov inc X, bl X Page 18 of 18