1 Chapter 6 Conditional Processing Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine.

Slides:



Advertisements
Similar presentations
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Advertisements

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Conditional Loop Instructions LOOPZ and LOOPE LOOPNZ.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 17, 2006.
CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University.
Flow Control Instructions
Conditional Processing If … then … else While … do; Repeat … until.
1 Flow Control Instructions and Addressing Modes Chapter 3.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Arithmetic Flags and Instructions
(Flow Control Instructions)
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 5 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
Comparison Instructions Test instruction –Performs an implied AND operation between each of the bits in 2 operands. Neither operand is modified. (Flags.
CSC 221 Computer Organization and Assembly Language Lecture 20: Conditional and Block Structures.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Bitwise and Logical Manipulations Assembly Language Programming University of Akron Dr. Tim Margush.
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
1 Conditional Processing Chapter 6 Adapted with many modifications from slides prepared by Professor Mario Marchand Computer Science Dept. University.
CSC 221 Computer Organization and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Homework Reading Labs PAL, pp
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Microprocessor and Assembly Language
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
Microprocessor and Assembly Language
(The Stack and Procedures)
Computer Organization and Assembly Language
Lecture 4 ( Assembly Language).
Flags Register & Jump Instruction
(The Stack and Procedures)
Shift & Rotate Instructions)
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Shift & Rotate Instructions)
Flow Control Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
EECE.3170 Microprocessor Systems Design I
High-level language structures
Computer Organization and Assembly Language
Assembly Language for Intel 8086 Jump Condition
(The Stack and Procedures)
Chapter 7 –Program Logic and Control
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
Computer Architecture and Assembly Language
Presentation transcript:

1 Chapter 6 Conditional Processing Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine

2 Conditional Processing n 6.1 Boolean and Comparison Instructions n 6.2 Conditional Jumps n 6.3 Conditional Loops n 6.4 High-Level Logic Structures

3 Logic Instructions n Syntax for AND, OR, XOR, and TEST: op-code destination, source n AND, OR and XOR perform the Boolean bitwise operation and store the result into destination. n TEST is just an AND but the result is not stored n Both operands must be of the same type u either byte, word or dword n Both operands cannot be memory u again: memory to memory operations are forbidden n They affect SF, OF, CF, ZF, PF according to the result of the operation (as usual)

4 Logic Instructions (cont.) n The source is often an immediate operand called a bit mask: used to fix certain bits to 0 or 1 n To clear a bit we use an AND since: u 0 AND b = 0 (b is cleared) u 1 AND b = b (b is conserved) n Ex: to clear the sign bit of AL without affecting the others, we do:  AND al,7Fh ;msb of AL is cleared since 7Fh = b

5 Logic Instructions (cont.) n To set (ie: set to 1) certain bits, we use OR: u 1 OR b = 1 (b is set) u 0 OR b = b (b is conserved) n To set the sign bit of AH, we do:  OR ah,80h n To test if ECX=0 we can do:  OR ecx,ecx u since this does not change the number in ECX and set ZF=1 if and only if ecx=0

6 Logic Instructions (cont.) n XOR can be used to complement, conserve, or clear certain bits because: u b XOR 1 = NOT(b) (b is complemented) u b XOR 0 = b (b is conserved) u b XOR b = 0 (b is cleared) n Ex: to initialize a register to 0 we can use a two-byte instruction: u XOR ax,ax u instead of the three-byte instruction:  MOV ax,0 This was faster on 8088's

7 Logic Instructions (cont.) n Using OR and XOR in strange ways: n XOR ax, ax MOV ax, 0 OR ax, ax CMP ax, 0 bytes /88 3 cycles 4 cycles cycles 2 cycles cycles 2 cycles cycle 1 cycle n Machine code 33 C0 B B C0 83 F8 00

8 Logic Instructions (cont.) n To convert from upper case to lower case we can use the usual method:  ADD dl,20h n But"A" = b "a" = b  OR dl,20h ;converts from upper to lower case  AND dl,0DFh ;converts from lower to upper case since DFh = b OR dl,20h and AND dh, 20h leaves lower case characters in dl unchanged and upper case characters in dh unchanged Bit 5

9 Logic Instructions (cont.) n To invert all the bits (ones complement): NOT destination u does not affect any flag and destination cannot be an immediate operand n To perform twos complement: NEG destination u affect SF, PF, ZF according to result u CF is set to 1 unless the result is 0 u OF=1 iff destination is the smallest negative number (ie: 80h for byte, 8000h for word...)

10 The CMP instruction CMP destination,source n Performs: destination - source but does not store the result of this subtraction into destination n But the flags are affected like SUB n Same restrictions on operands as SUB n Very often used just before performing a conditional jump n CMP is similar to TEST except TEST does AND operation

11 Conditional Jumps n A conditional jump transfers control to the destination_label when a flag condition is true. Syntax: u Jxxx destination_label u destination_label must be within -128 to +127 bytes from the current location. (Short jump.) n Groupings of conditional jumps: u General comparison jumps (no interpretation) u Unsigned comparison jumps u Signed comparison jumps

12 General Comparison Jumps

13 Unsigned Comparison Jumps The unsigned interpretation is used

14 Signed Comparison Jumps The signed interpretation is used SF = OF and ZF = 0

15 Using conditional jumps Consider mov ax, x sub ax, y jz SpecialCase n If x equals y then ax is zero and the jump is taken n If x is not equal to y then ax is not zero and the jump is not taken We could have used je SpecialCase

16 Using conditional jumps n Simpler if CMP is used before a Jxxx n To branch to exit when AX > BX under a signed interpretation:  cmp ax,bx u jg exit  If AX=1, BX=FFFFh, the jump is taken n To branch to exit when AX > BX under a unsigned interpretation:  cmp ax,bx u ja exit u if AX=1 and BX =FFFFh, the jump is not taken

17 Unconditional Jump n To jump unconditionally: jmp destination_label u where the destination_label can occur anywhere within the same segment n This permits us to circumvent the 128-byte range limitation of conditional jumps.

18 Loops larger than 128 bytes n Ex: suppose we want to do:  TOP:...more than 128 bytes of instructions... cmp ax,bx jne TOP n illegal n We do instead: TOP:...more than 128 bytes of instructions... cmp ax,bx je EXIT jmp TOP EXIT: MASM will automatically convert the source code on the left side to the machine code for right side

19 The Isalpha Procedure ; Isalpha sets ZF = 1 iff the character ; in AL is alphabetic. Isalpha proc push ax ; save AX and al, b ; clear bit 5 cmp al,'A' ; check 'A'..'Z' range jb B1 cmp al,'Z' ja B1 test ax,0 ; ZF = 1 B1: pop ax ; restore AX ret Isalpha endp

20 Ex: checking for alphabetic input n The Isalpha procedure sets ZF to 1 iff the character in AL is alphabetic (upper or lower case). u When comparing characters: use unsigned comparison jumps u Trick: clear bit #5 and check if ‘A’ <= char <= ‘Z’ (converts lower case to upper case)  cmp al, ‘A’ will clear ZF when char < ‘A’  cmp al, ‘Z’ will clear ZF when char > ‘Z’  test ax, 0 is executed only if ‘A’ <= char <= ‘Z’ and sets ZF to 1

21 Ex: file encryption n The following property of XOR b XOR 0 = b (bit b is conserved) b XOR 1 = NOT(b) (bit b is complemented) can be used to encrypt files n If a character is stored in AL, XORing AL once encrypts the character. Using it the second time decrypts (restores) the character.  key = 137 ;a 1-byte secret key  xor al,key ;AL is encrypted  xor al,key ;AL is restored to its original value

22 Ex: file encryption (cont) n When AH=6 and DL=0FFh, INT 21h loads AL with the next char from the keyboard buffer or sets ZF to 1 when keyboard buffer is empty u (unlike function 1) it does not "busy" wait until a char enters the keyboard buffer n DOS input functions can be redirected from a file on the DOS command line n Hence, encrypt.asm can be used to encrypt and decrypt a fileencrypt.asm u C> encrypt coded (encryption) u C> encrypt my_file (decryption)

23 main proc ;encrypt.asm mov mov ds,ax L1: mov ah,6 ; direct console input mov dl,0FFh ; don't wait for char int 21h ; AL = character jz L2 ; quit if ZF = 1 (EOF) xor al,key ; or any other value mov ah,2 ; write to output mov dl,al int 21h jmp L1 ; repeat the loop L2: mov ax,4C00h ; return to DOS int 21h main endp To encode and decode a file

24 Programming the Keyboard Status n Keyboard status byte: 0040:0017h n Bit assignments: ; 0 Right shift key down ; 1 Left shift key down ; 2 Left or right ctrl key down ; 3 Left or right alt key down ; 4 Scroll lock (toggle) ; 5 Num lock (toggle) ; 6 Caps lock (toggle) ; 7 Insert (toggle)

25 Programming the Keyboard Status n The keyboard status is stored in location 0040:0017. NumLock is bit 5. Lets turn it on  Numlock = b PUSH DS ; if needed MOV AX, 0040h MOV DS, AX MOV BX, 17h OR BYTE PTR [BX], Numlock; Set Numlock POP DS ; if needed

26 Programming the Keyboard Status n Easier: Assume that ES (extra segment) is not being used. Use the ES instead of DS n Numlock = b... mov ax, 40 ; this may only have mov es, ax ; to be done once... MOV BX, 17h OR BYTE PTR ES:[BX], Numloc ; Set Numlock

27 Programming the Keyboard Status n Other operations mov ax, 40 ; this may only need mov es, ax ; to be done once Numlock = b NotNumlock = not Numlock... MOV BX, 17h AND BYTE PTR ES:[BX], NotNumlock ; Clear Numlock OR BYTE PTR ES:[BX], Numlock ; Set Numlock XOR BYTE PTR ES:[BX], Numlock ; Toggle Numlock TEST BYTE PTR ES:[BX], Numlock ; Test Numlock See keyboard.asm. It continually reads the keyboard status byte and reports the statuskeyboard.asm

28 High-Level Flow Control Structures n High-level languages uses high-level structures such as if-then-else, case, while... to control the flow of execution u algorithms are normally expressed in terms of these high-level structures n Processors only provide conditional and unconditional jumps and loops u thus we need to decompose the high-level control flow structures into low-level ones n We give here a few examples; see textbook for more

29 If-Then-Else n Example: if (op1 < op2) then statement 1 else statement 2 end if n Analysis: u there is a jxxx to else when op1 >= op2 u there is a jump from end of statement 1 to end_if n ASM solution for signed comparison: cmp op1,op2 jge else_  statement 1 jmp end_if else_:  statement 2  end_if: n Note: “else” is a ASM reserved word. We use “else_” instead

30 While n Example : do while (op1 < op2) statement end do n Analysis: u jxxx to end_do when op1 >= op2 u jump from before the end_do to do_ while n AMS solution for an unsigned comparison: do_while: cmp op1,op2  jae end_do statement jmp do_while end_do:

31 Case n Example: case input of ‘A’ :procA ‘B’ :procB ‘C’ :procC end case n Analysis: cmp and jxxx for each case ASM solution: cmp input,’A’ jne L1 call procA jmp L3 L1: cmp input,’B’ jne L2 call procB jmp L3 L2: cmp input,’C’ jne L3 call procC L3:

32 Conditional Loops n The LOOPZ and LOOPE (loop if zero, loop if equal) continues a loop while ZF = 1 and CX > 0 u CX is first decremented, then the condition is tested (a trivial extension of LOOP) n LOOPNZ and LOOPNE continues the loop while ZF = 0 and CX > 0 n Syntax (same as LOOP): loopxx destination_label n ECX is used when in the 32 bit mode

33 Ex: scanning an integer array until a nonzero value is found mov bx,offset Warr-2 ;Warr is a word array mov cx,N ;of N elements next: add bx,2 cmp word ptr [bx], 0 loopz next ;loop until nonzero value is found n Note: it is an error to inverse the CMP and ADD operations (ADD affects the flags!)