IBM PC Hardware CPU 8088, Pentium... ALU (Arithmetic and Logic Unit) Registers. CU (Control Unit) IP.

Similar documents
EC 333 Microprocessor and Interfacing Techniques (3+1)

Basic Assembly Instructions

Lecture 9. INC and DEC. INC/DEC Examples ADD. Arithmetic Operations Overflow Multiply and Divide

Lecture 8: Control Structures. Comparing Values. Flags Set by CMP. Example. What can we compare? CMP Examples

We will first study the basic instructions for doing multiplications and divisions

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ

Arithmetic Instructions

CS-202 Microprocessor and Assembly Language

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

The statement above assumes that the PCMAC.INC file is in the current directory. The full path of the file can also be given:

Basic Assembly SYSC-3006

Intel 8086: Instruction Set

UNIT 4. Modular Programming

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Selection and Iteration. Chapter 7 S. Dandamudi

Lecture (08) x86 programming 7

Defining and Using Simple Data Types

Signed number Arithmetic. Negative number is represented as

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

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

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

Mnem. Meaning Format Operation Flags affected ADD Addition ADD D,S (D) (S)+(D) (CF) Carry ADC Add with ADC D,C (D) (S)+(D)+(CF) O,S,Z,A,P,C

Lecture 16: Passing Parameters on the Stack. Push Examples. Pop Examples. CALL and RET

db "Please enter up to 256 characters (press Enter Key to finish): ",0dh,0ah,'$'

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

Arithmetic and Logic Instructions And Programs

8086 INSTRUCTION SET

Week /8086 Microprocessor Programming

Week /8086 Microprocessor Programming I

Introduction to 8086 Assembly

Summer 2003 Lecture 4 06/14/03

Assembly Language: IA-32 Instructions

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

ECOM Computer Organization and Assembly Language. Computer Engineering Department CHAPTER 7. Integer Arithmetic

ADVANCE MICROPROCESSOR & INTERFACING

8086 Programming. Multiplication Instructions. Multiplication can be performed on signed and unsigned numbers.

Practical Malware Analysis

Computer Architecture and System Software Lecture 04: Floating Points & Intro to Assembly

Computer Architecture..Second Year (Sem.2).Lecture(4) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

Integer Arithmetic Part2

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION 80x86 Instructions

Lab 6: Conditional Processing

Real instruction set architectures. Part 2: a representative sample

LAB 5 Arithmetic Operations Simple Calculator

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

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

PESIT Bangalore South Campus

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

APPENDIX C INSTRUCTION SET DESCRIPTIONS

CPU. IBM PC and compatible Memory Structure

Instructions moving data

Section 001. Read this before starting!

Experiment 3 3 Basic Input Output

BLDEA S V.P. DR. P.G. HALAKATTI COLLEGE OF ENGINEERING & TECHNOLOGY, VIJAYAPURA

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

IFE: Course in Low Level Programing. Lecture 6

Mr. Sapan Naik 1. Babu Madhav Institute of Information Technology, UTU

Assembler lecture 4 S.Šimoňák, DCI FEEI TU of Košice

1-Operand instruction types 1 INC/ DEC/ NOT/NEG R/M. 2 PUSH/ POP R16/M16/SR/F 2 x ( ) = 74 opcodes 3 MUL/ IMUL/ DIV/ DIV R/M

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

CONTENTS. 1. Display a Message Display a one Digit Number Accept a Character from keyboard and display the character 4

EEM336 Microprocessors I. Arithmetic and Logic Instructions

Lesson 1. Fundamentals of assembly language

Ethical Hacking. Assembly Language Tutorial

SOEN228, Winter Revision 1.2 Date: October 25,

ORG ; TWO. Assembly Language Programming

Program Control Instructions


Ex : Write an ALP to evaluate x(y + z) where x = 10H, y = 20H and z = 30H and store the result in a memory location 54000H.

Computer Organisation CS303

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

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

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

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

Module 3 Instruction Set Architecture (ISA)

Microcomputer Architecture..Second Year (Sem.2).Lecture(2) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

CC411: Introduction To Microprocessors

8088/8086 Programming Integer Instructions and Computations

CHAPTER SEVENTEEN Assemblers versus Compilers. Intel 80x86 Assembly Language

ELEC 242 Time Delay Procedure

Section 002. Read this before starting!

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

Computer Science Final Examination Wednesday December 13 th 2006

Introduction to 8086 Assembly

from WRITE GREAT CODE Volume 2: Thinking Low-Level, Writing High-Level ONLINE APPENDIX A The Minimal 80x86 Instruction Set by Randall Hyde

US06CCSC04: Introduction to Microprocessors and Assembly Language UNIT 3: Assembly Language Instructions II

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Experiment N o 1. 1 Introduction to Assembly Language Programming

3.1 DATA MOVEMENT INSTRUCTIONS 45

Branching and Looping

Chapter 6 (Part a) Conditional Processing

Lecture (07) x86 programming 6

9/25/ Software & Hardware Architecture

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

Logic Instructions. Basic Logic Instructions (AND, OR, XOR, TEST, NOT, NEG) Shift and Rotate instructions (SHL, SAL, SHR, SAR) Segment 4A

L1 Remember, L2 Understand, L3 - Apply, L4 Analyze, L5 Evaluate, L6 Create

Transcription:

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