Lecture 4 Basic Instructions Dr. Dimitrios S. Nikolopoulos CSL/UIUC.

Slides:



Advertisements
Similar presentations
Department of Computer Science and Software Engineering
Advertisements

ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
80x86 Instruction Set Dr. Qiang Lin.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
8086 : INSTRUCTION SET By, Pramod Sunagar Assistant Professor
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 5 Arithmetic and Logic Instructions.
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Shift and Rotate Instructions
Microcomputer & Interfacing Lecture 3
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
An Introduction to 8086 Microprocessor.
Ch. 7 Logic, Shift and Rotate instr.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
Types of Registers (8086 Microprocessor Based)
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Department of Computer Science and Software Engineering
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
ECE291 Computer Engineering II Lecture 4 Josh Potts University of Illinois at Urbana- Champaign.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Arithmetic and Logic Instructions. Outline Declarations Moving data The Flags register Logic instructions Addition & subtraction.
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
3.4 Addressing modes Specify the operand to be used. To generate an address, a segment register is used also. Immediate addressing: the operand is a number.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
ECE291 Computer Engineering II Lecture 4 Josh Potts University of Illinois at Urbana- Champaign.
Internal Programming Architecture or Model
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.
The Microprocessor & Its Architecture A Course in Microprocessor Electrical Engineering Department Universitas 17 Agustus 1945 Jakarta.
I NTEL 8086 M icroprocessor بسم الله الرحمن الرحيم 1.
Instruction set Architecture
Data Transfers, Addressing, and Arithmetic
16.317: Microprocessor System Design I
UNIT Architecture M.Brindha AP/EIE
Introduction to 8086 Microprocessor
8086 Microprocessor.
Microprocessor Systems Design I
ADDRESSING MODES.
Microprocessor Systems Design I
Intel 8086 MICROPROCESSOR Architecture.
Basic Microprocessor Architecture
Machine control instruction
Assembly Language Programming Part 2
ADDRESSING MODES.
Intel 8088 (8086) Microprocessor Structure
Microcomputer & Interfacing Lecture 1
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
Symbolic Instruction and Addressing
X86’s instruction sets.
Introduction to Assembly Language
Intel 8088 (8086) Microprocessor Structure
8086 Registers Module M14.2 Sections 9.2, 10.1.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
(Array and Addressing Modes)
CS 301 Fall 2002 Computer Organization
Shift & Rotate Instructions)
The Microprocessor & Its Architecture
Symbolic Instruction and Addressing
(Array and Addressing Modes)
Shift & Rotate Instructions)
CNET 315 Microprocessor & Assembly Language
Chapter 5 Arithmetic and Logic Instructions
Chapter 6 –Symbolic Instruction and Addressing
Chapter 8: Instruction Set 8086 CPU Architecture
Shift and Rotate Instructions.
Ch. 5 – Intel 8086 – study details from Yu & Marut
(Array and Addressing Modes)
Presentation transcript:

Lecture 4 Basic Instructions Dr. Dimitrios S. Nikolopoulos CSL/UIUC

Outline Declarations Moving data The flags register Logic instructions Addition&subtraction

Declarations SEGMENT code myvar1DW1234h; define word variable ; value=1234h myvar2DW1234; define word variable ; value=1234d=4D2h myvar3RESW; define word variable ; value not specified myvar4DW0ABCDh; define word variable ; value = ABCDh ; note the leading zero ece291msgDB “ ECE291 is great! ” ; define strings as series of bytes ; this is not one byte but 16!..start; denotes execution starting point

It’s all bytes stored in memory CS – Code segment begins CS:IP D2 00 ABCD 43 = ‘C’45 = ‘E’ 32 = ‘2’45 = ‘E’ 31 = ‘1’39 = ‘9‘ 69 = ‘i‘20 = ‘ ’ B D F A C E ‘ ’ = ‘s’ 72 = ‘r’67 = ‘g’ 61 = ‘a’65 = ‘e’ 21 = ‘!’74 = ‘t’ ?? B 1D 1F A 1C 1E myvar1 myvar2 myvar3 myvar4 ece291msg

Moving data mov ax, cs; make data seg same as code seg mov ds, ax mov ax, [myvar2]; move value of myvar2 to ax ; same as mov ax, [2] ; ax now contains 04D2h mov si, myvar2; si now points to myvar2 ; it contains 0002h mov ax, [si]; move value pointed to by si to ax ; ax again contains 04D2h ; this was an indirect reference

Moving Data mov bx, ece291msg; bx now points to the beginning of ; out “ ECE291 is great ” string. ; bx points to the first ‘ E ’ ; bx contains 0008h dec byte [bx + 1]; new instruction! arg = arg – 1 ; Just changed the C in “ ECE ” to a B mov si, 1; now lets use SI as an index inc byte [ece291msg + si]; new instruction! arg = arg + 1 ; evaluates to inc [8 + 1] = inc [9] ; B becomes a C again!

Moving Data ; Memory can be addressed using four registers only!!! ; SI -> offsets from DS ; DI -> offsets from DS ; BX -> offsets from DS ; BP -> offsets from SS mov ax, [bx]; ax = word in memory pointed to by bx mov al, [bx]; al = byte in memory pointed to by bx mov ah, [si]; ah = byte in memory pointed to by si mov cx, [di]; cx = word in memory pointed to by di mov ax, [bp]; ax = [SS:BP] Stack Operation! ; In addition, BX + SI, BX + DI, BP + SI, and BP + DI are allowed mov ax, [bx + si]; ax = [ds:bx+si] mov ch, [bp + di]; ch = [ss:bp+di]

Moving Data ; Furthermore, a fixed 8- or 16-bit displacement can be added mov ax, [23h]; ax = word at [DS:23h] mov ah, [bx + 5]; ah = byte at [DS:bx+5] mov ax, [bx + si + 107]; ax = word at [DS:bx+si+107] mov ax, [bp + di + 42]; ax = word at [SS:bp+di+42] ; Remember that memory to memory moves are illegal! mov [bx], [si]; BAD BAD BAD mov [di], [si]; BAD BAD BAD ; Special case with stack operations pop word [myvar]; moves data from top of stack to ; memory variable myvar

Flags register The flags register is a collection of bits that determines the state of the processor after an operation Flag bits change after arithmetic and logic instructions (e.g. carry, overflow, zero, sign, parity) 9 out of the 16 bits of the FLAGS register have meaning in the 8086, 11 out of the 32 bits in the EFLAGS register of the 80386, and 12 out of the 32 bits in the EFLAGS register of the and higher

Flags register 8086, 8088, , 80486DX 80486SX AC (Alignment check) (VM) Virtual mode (RF) Resume (NT) Nested task (IOPL) Input/output privilege level (O) Overflow (D) Direction (I) Interrupt (T) Trace (S) Sign (Z) Zero (A) Auxiliary Carry (P) Parity (C) Carry

Logic instructions NOT, AND, OR, XOR NOT A = ~A AND A,B = A& =B OR A,B=A |= B XOR A,B ^= A NOT does not affect any flags The other instructions affect –C (carry flag, they clear it) –O (overflow flag, they clear it) –Z (zero flag, they set it if result is 0) –S (sign flag, takes the high order bit of the result) –P (parity, number of 1’s in the result) –A (auxiliary carry flag)

Examples of logic instructions NOT AL AL AL BL AND AL, BL AL BL OR AL, BL AL BL XOR AL, BL AL BL AND AL, BL AL BL OR AL, BL AL

Practical uses of AND/OR instructions Apply a bit mask to data –Masks are used to force certain bits to specific values Example –In x86 operating systems virtual memory is organized in pages, I.e. chunks of 4096 (1000h) consecutive bytes, aligned with an address which is an integer multiple of the page size –Find the beginning of the page that contains address B9A2C07Eh –Mask=page_size = 1000h –AND B9A2C07E, mask = B9A2C000

The TEST instruction TEST is a non-destructive version of AND –Logically AND two operands like AND –Modify FLAG bits like AND –Doesn’t modify the contents of the destination –TEST AL,1 sets flags like AND AL,1 but does not modify AL –Useful in jump instructions

Shift left Syntax SHL reg, count (immediate) SHL reg, CL (count in register) Moves the left operand a bit position to the left as many times as specified by count or CL The empty positions are filled with 0’s The higher order bit is stored in the C flag The most efficient way to multiply by 2! 8086 and 8088 had immediate shl by 1 instructions 0 RegisterC SHL

Example ; Pack the lower nibbles from AH and AL into a single byte in AL ; for example AH = and AL = and al, 0Fh; clears upper nibble in AL ; AL = shl ah, 4; Moves lower nibble in AH up ; AH = or al, ah; combines nibbles ; AL = ; Mission accomplished

Shift right Syntax SHR reg, count (immediate)Syntax SHR reg, count (immediate) SHR reg, CL (count in register) Moves the left operand a bit position to the right as many times as specified by count or CLMoves the left operand a bit position to the right as many times as specified by count or CL The empty positions are filled with 0’sThe empty positions are filled with 0’s The lower order bit is stored in the C flagThe lower order bit is stored in the C flag The most efficient way to divide by 2!The most efficient way to divide by 2! 8086 and 8088 had immediate shr by 1 instructions8086 and 8088 had immediate shr by 1 instructions RegisterC SHR

Shift arithmetic right Syntax: SAR reg, count SAR reg, CL Moves each bit of the operant one position to the right as many times as specified by count/CL Lower order bit shifts to the carry flag High order bit is replicated RegisterC SAR

Shift arithmetic right Main purpose is to perform a signed division by some power of two MOV AX, -15 SAR AX, 1; result = -8 In and later SAR can be used to sign extend one register into another MOV AH, AL; assume AL = SAR AH, 7; AX =

Rotate through carry L/R Syntax:RCL reg, count (immediate) RCL reg, CL (count in register) Rotate bits to the left through the carry flag Bit in the carry flag is written back to bit 0 Syntax:RCR reg, count (immediate) RCR reg, CL (count in register) Rotate bits to the right through the carry flag Bit in the carry flag is written back to H.O. bit RCLRCR

Rotate left/right Syntax:ROL reg, count (immediate)Syntax:ROL reg, count (immediate) ROL reg, CL (count in register) Rotate bits to the leftRotate bits to the left H.O. bit goes to the L.O. bit and the carry flagH.O. bit goes to the L.O. bit and the carry flag Syntax:ROR reg, count (immediate)Syntax:ROR reg, count (immediate) ROR reg, CL (count in register) Rotate bits to the rightRotate bits to the right L.O. bit goes to the H.O. bit and the carry flagL.O. bit goes to the H.O. bit and the carry flag ROLROR

Example mov ax,3; Initial register valuesAX = mov bx,5;BX = or ax,9; ax <- ax | AX = and ax, b; ax <- ax & AX = xor ax,0FFh; ax <- ax ^ AX = neg ax; ax <- (-ax)AX = not ax; ax <- (~ax)AX = Or ax,1; ax <- ax | AX = shl ax,1; logical shift left by 1 bitAX = shr ax,1; logical shift right by 1 bitAX = ror ax,1; rotate right (LSB=MSB)AX = rol ax,1; rotate left (MSB=LSB)AX = mov cl,3; Use CL to shift 3 bitsCL = shr ax,cl; Divide AX by 8AX = mov cl,3; Use CL to shift 3 bitsCL = shl bx,cl; Multiply BX by 8BX =

Addition Syntax: ADD A,B –A, B can be register or memory, but not both memory –B can also be immediate data Examples –ADD AX, BX; register addition –ADD AX, 5h; immediate addition –ADD [BX],AX; addition to memory location –ADD AX,[BX]; memory location added to register –ADD DI,MYVAR; memory offset added to the register Flags modified: S, C, Z, O, A, P

Example ; multiply AX by the decimal 10 (1010b) ; (multiply by 2 and 8, then add results) shl ax, 1; AX x 2 mov bx, ax; save 2AX shl ax, 2; 2 x (original AX) x 4 = 8 x (orig AX) add ax, bx; 2AX + 8AX = 10AX ; multiply AX by decimal 18 (10010b) ; (multiply by 2 and 16, then add results) shl ax, 1; AX x 2 mov bx, ax; save 2AX shl ax, 3; 2AX x 2 x 2 x 2 = 16AX add ax, bx; 2AX + 16AX = 18AX

Increment Syntax: INC A –A can be register or memory location Examples: –INC AX; AX=AX+1 –INC byte[BX]; memory location increased by 1 –INC word[BX]; memory location increased by 1 Flags modified: S, Z, O, A, P But not the carry flag!

Add with carry Syntax:ADC A,B –A, B can be registers or memory –B can also be immediate Function: A = A + B + carry flag Used to add numbers larger than 16 bits (in 16-bit mode) or larger than 32 bits in (32-bit mode) Examples –ADD AX,CX; produces a 32-bit sum –ADC BX,DX; of BX:AX + DX:CX -> BX:AX Flags modified: S,C,A,Z,O,P

Subtraction Syntax:SUB A,B –A, B can be register or memory (not both memory) –B can be immediate Function: A=A-B –Carry flag is interpreted as a borrow request from H.O. bit Flags modified S,C,A,Z,P,O Example: MOV CH, 22h SUB CH, 34h; result = -12h, S=1 (negative), Z=0, C=1 ; (borrow from high order bit), P=0 (even ; parity)

Decrement Syntax:DEC A –A can be register or memory Function: A=A-1 Examples –DEC AX; AX=AX-1 –DEC BYTE [BX]; memory location decreased by 1 –DEC WORD [BX] ; memory location decreased by 1 Flags modified: S,A,Z,P,O Carry flag not modified!

Subtract with borrow Syntax:SBBA,B –A, B can be register or memory (not both memory) –B can be immediate Function:A=A - B - carry flag –Used to subtract numbers which are wider than 16 bits (in 16-bit mode) or wider than 32 bits (in 32-bit mode) Flags modified S,C,A,Z,P,O Examples: SUB AX, DI; this subtracts the 32-bit numbers SBB BX, SI; BX:AX-SI:DI -> BX:AX

The carry flag Indicates a carry in the H.O. bit after addition or a borrow after subtraction Carry is set when an unsigned number goes out of range Example (8 bits) –FFh + 1h = 00h with C=1 –02h – 03h = FFh with C=1

The overflow flag Condition set when signed numbers go out of range Example (8 bits) –7Fh + 1h = 80h (wanted +128 but got –128) –80h – 1h = 7Fh (wanted –127 but got +127)

Overflows and carries mov ax,0fffeh; interpreted as unsigned add ax,3 ; C = 1, O = Unsigned Overflow Condition mov ax,0FFFEh ; -2 interpreted as signed add ax,3; C = 1, O = Okay as signed mov bx,07FFFh; interpreted as signed add bx,1; C = 0, O = Signed Overflow Condition mov bx,07FFFh; interpreted as unsigned add bx,1; C = 0, O = Okay as unsigned mov ax,07h; 7 interpreted as either signed or unsigned add ax,03h; C = 0, O = Okay as either