Computer Department Chapter 7. Created By: Eng. Ahmed M. Ayash Modified and Presented by: Eng. Eihab S. El-Radie. Chapter 7

Similar documents
Integer Arithmetic. Shift and rotate. Overview. Shift and Rotate Instructions

Integer Arithmetic. Pu-Jen Cheng. Adapted from the slides prepared by Kip Irvine for the book, Assembly Language for Intel-Based Computers, 5th Ed.

Integer Arithmetic. Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/17. with slides by Kip Irvine

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

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

Lab Session 08. To understand the use of Shift and Rotate instructions. To be able to differentiate between Arithmetic shift and Logical shift.

Shift and Rotate Instructions

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

Lecture 9. INC and DEC. INC/DEC Examples ADD. ADD Examples SUB. INC adds one to a single operand DEC decrements one from a single operand

Chapter 7 Integer Arithmetic

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

Introduction to 8086 Assembly

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

Week /8086 Microprocessor Programming I

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.

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

Chapter 5. Real-Mode 80386DX Microprocessor Programming 1 Part 2. The 80386, 80486, and Prentium Processors,Triebel Prof. Yan Luo, UMass Lowell 1

Instructions moving data

Microprocessor & Computer Architecture / Lecture

Lecture (08) x86 programming 7

Assembly Language LAB

Data Transfers, Addressing, and Arithmetic. Part 2

Arithmetic and Logic Instructions And Programs

Q1: Multiple choice / 20 Q2: Protected mode memory accesses

Logical and Bit Operations. Chapter 8 S. Dandamudi

Computer Architecture and Assembly Language. Practical Session 3

Kingdom of Saudi Arabia Ministry of Higher Education. Taif University. Faculty of Computers & Information Systems

Q1: Multiple choice / 20 Q2: Memory addressing / 40 Q3: Assembly language / 40 TOTAL SCORE / 100

Q1: Multiple choice / 20 Q2: Data transfers and memory addressing

16.317: Microprocessor Systems Design I Spring 2014

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

Week /8086 Microprocessor Programming II

Computer Architecture and System Programming Laboratory. TA Session 3

Logical and bit operations

Assignment no:4 on chapter no :3 : Instruction set of 8086

Bit Operations. Ned Nedialkov. McMaster University Canada. SE 3F03 February 2014

Code segment Stack segment

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

8088/8086 Programming Integer Instructions and Computations

Basic Assembly SYSC-3006

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Overview of Assembly Language

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Chapter 12. Selected Pentium Instructions

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

Lecture Notes on Assembly Language - J. Vaughan

Defining and Using Simple Data Types

Intel 8086: Instruction Set

Week /8086 Microprocessor Programming

16.317: Microprocessor Systems Design I Fall 2013

EECE.3170: Microprocessor Systems Design I Spring 2016

CS241 Computer Organization Spring 2015 IA

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

Signed number Arithmetic. Negative number is represented as

International Islamic University Chittagong (IIUC) Department of Electrical and Electronic Engineering (EEE)

Integer Arithmetic Part2

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

Q1: Define a character string named CO_NAME containing "Internet Services" as a constant?

The x86 Architecture

Experiment 3 3 Basic Input Output

3.1 DATA MOVEMENT INSTRUCTIONS 45

CS/ECE/EEE/INSTR F241 MICROPROCESSOR PROGRAMMING & INTERFACING MODULE 4: 80X86 INSTRUCTION SET QUESTIONS ANUPAMA KR BITS, PILANI KK BIRLA GOA CAMPUS

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.

A4 Sample Solution Ch3

Module 3 Instruction Set Architecture (ISA)

Basic Pentium Instructions. October 18

CS401 Assembly Language Solved MCQS From Midterm Papers

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

EC 333 Microprocessor and Interfacing Techniques (3+1)

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

SOEN228, Winter Revision 1.2 Date: October 25,

Chapter 3: Addressing Modes

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

8086 INSTRUCTION SET

b) List the 16 Bit register pairs of 8085?(Any 2 pair, 1 Mark each) 2M Ans: The valid 16 bit register pair of 8085 are

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

Assembly Language Each statement in an assembly language program consists of four parts or fields.

Lecture 2 Assembly Language

Introduction to IA-32. Jo, Heeseung

INTRODUCTION TO IA-32. Jo, Heeseung

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 6: Conditional Processing

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

ADVANCE MICROPROCESSOR & INTERFACING

PESIT Bangalore South Campus

COMPUTER ENGINEERING DEPARTMENT

Chapter 6 (Part a) Conditional Processing

Assembly Language LAB

CMSC 313 Lecture 07. Short vs Near Jumps Logical (bit manipulation) Instructions AND, OR, NOT, SHL, SHR, SAL, SAR, ROL, ROR, RCL, RCR

Assembly Language LAB

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

/ 30 Q3: Arithmetic instructions / 30 Q4: Logical instructions / 20 TOTAL SCORE / 100 Q5: EXTRA CREDIT / 10

CS-202 Microprocessor and Assembly Language

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

Assembly Language Lab # 6

Language of x86 processor family

UNIT II. 2 Marks. 2. List out the various register of IA- 32 Register structure. 3. Write about General purpose registers

Addressing Modes on the x86

Q1: Multiple choice / 20 Q2: Memory addressing / 40 Q3: Assembly language / 40 TOTAL SCORE / 100

Arithmetic Instructions

Assembly Language LAB

Transcription:

Islamic University Of Gaza Assembly Language Faculty of Engineering Discussion Computer Department Chapter 7 Created By: Eng. Ahmed M. Ayash Modified and Presented by: Eng. Eihab S. El-Radie Chapter 7 Integer Arithmetic - Part1 7.2: Shift and Rotate Instructions Q Which instruction shifts each bit in an operand to the left and copies the highest bit into both the Carry flag and the lowest bit position? A ROL. Q Which instruction shifts each bit to the right, copies the lowest bit into the Carry flag, and copies the Carry flag into the highest bit position? A RCR. Q Write a sequence of shift instructions that cause AX to be sign-extended into EAX. In other words, the sign bit of AX is copied into the upper 16 bits of EAX. A First, shift EAX 16 bits to the left. Next, shift it arithmetically 16 bits to the right: shl eax,16 sar eax,16 Q Which instruction performs the following operation (CF = Carry flag)? Before: CF, AL = 1 11010101 After: CF, AL = 1 10101011 A RCL. Q Suppose the instruction set contained no rotate instructions. Show how we might use SHR and a conditional jump instruction to rotate the contents of the AL register one position to the right. A shr al,1 ; shift AL into Carry flag jnc next ; Carry flag set? or al,80h ; yes: set highest bit next: ; no: do nothing 1

Q What happens to the Carry flag when the SHR AX,1 instruction is executed? A The Carry flag receives the lowest bit of AX (before the shift). Q Write a logical shift instruction that multiplies the contents of EAX by 16. A shl eax,4 Q Write a logical shift instruction that divides EBX by 4. A shr ebx,2 Q Write a single rotate instruction that exchanges the high and low halves of the DL register. A ror dl,4 ;(or: rol dl,4) Q Write a SHLD instruction that shifts the highest bit of the AX register into the lowest bit position of DX and shifts DX one bit to the left. A shld dx,ax,1 Q In the following code sequence, show the value of AL after each shift or rotate instruction has executed: shr al,1 ; a. sar al,1 ; b. sar al,4 ; c. rol al,1 ; d. A (a) 6Ah (b) 0EAh (c) 0FDh (d) A9h Q In the following code sequence, show the value of AL after each shift or rotate instruction has executed: ror al,3 ; a. rol al,7 ; b. stc rcl al,1 ; c. stc rcr al,3 ; d. A (a) 9Ah (b) 6Ah (c) 0A9h (d) 3Ah 2

Q Write a series of instructions that shift the lowest bit of AX into the highest bit of BX without using the SHRD instruction. Next, perform the same operation using SHRD. A shr ax,1 rcr bx,1 ; Using SHRD: shrd bx,ax,1 ; shift AX into Carry flag ; shift Carry flag into BX Q One way to calculate the parity of a 32-bit number in EAX is to use a loop that shifts each bit into the Carry flag and accumulates a count of the number of times the Carry flag was set. Write a code that does this, and set the Parity flag accordingly. A mov ecx,32 ; loop counter mov bl,0 ; counts the '1' bits L1: shr eax,1 ; shift into Carry flag jnc L2 ; Carry flag set? inc bl ; yes: add to bit count L2: loop L1 ; continue loop ; if BL is odd, clear the parity flag ; if BL is even, set the parity flag shr bl,1 jc odd mov bh,0 or bh,0 ; PF = 1 jmp next odd: mov bh,1 or bh,1 ; PF = 0 next: 7.3: Shift and Rotate Applications Q Write a sequence of instructions that shift three memory bytes to the right by 1 bit position. Use the following data definition: bytearray BYTE 81h,20h,33h A This problem requires us to start with the high-order byte and work our way down to the lowest byte: bytearray BYTE 81h,20h,33h shr bytearray+2,1 rcr bytearray+1,1 rcr bytearray,1 Q Write a sequence of instructions that shift three memory words to the left by 1 bit position. Use the following data definition: wordarray WORD 810Dh, 0C064h,93ABh A This problem requires us to start with the low-order word and work our way up to the highest word: 3

wordarray WORD 810Dh,0C064h,93ABh shl wordarray,1 rcl wordarray+2,1 rcl wordarray+4,1 Q Write ASM instructions that calculate EAX * 24 using binary multiplication. A The multiplier (24) can be factored into 16 + 8 = (2 4 +2 3 ): mov ebx,eax ; save a copy of eax shl eax,4 ; multiply by 16 shl ebx,3 ; multiply by 8 add eax,ebx ; add the products Q Write ASM instructions that calculate EAX * 21 using binary multiplication. A The multiplier (21) can be factored into 16 + 4 + 1 = (24+22+20): mov ebx,eax ; save a copy of eax mov ecx,eax ; save another copy of eax shl eax,4 ; multiply by 16 shl ebx,2 ; multiply by 4 add eax,ebx ; add the products add eax,ecx ; add original value of eax Displaying Binary Bits Algorithm: Shift MSB into the Carry flag; If CF = 1, append a "1" character to a string; otherwise, append a "0" character. Repeat in a loop, 32 times..data buffer db 32 DUP(0) mov cx,32 mov si,offset buffer L1: shl eax,1 mov BYTE PTR [si],'0' jnc L2 mov BYTE PTR [si],'1' L2: inc si loop L1 Q What change would you make to the previous algorithm in order to display the binary bits in reverse order? A Change the instruction at label L1 to shr eax,1 Isolating MS-DOS File Date Fields 4

The MS-DOS file date field packs the year, month, and day into 16 bits, If a file was last modified on March 10, 1999, the file s date stamp would appear as follows in the DX register (the year number is relative to 1980): o Isolate the Day field: mov al,dl ; make a copy of DL and al,00011111b ; clear bits 5-7 mov day,al ; save in day o Isolate the Month field: mov ax,dx ; make a copy of DX shr ax,5 ; shift right 5 bits and al,00001111b ; clear bits 4-7 mov month,al ; save in month o Isolate the Year field: mov al,dh ; make a copy of DH shr al,1 ; shift right one position mov ah,0 ; clear AH to zeros add ax,1980 ; year is relative to 1980 mov year,ax ; save in year Q The time stamp field of a file directory entry uses bits 0 through 4 for the seconds, bits 5 through 10 for the minutes, and bits 11 through 15 for the hours. Write instructions that extract the minutes and copy the value to a byte variable named bminutes. A We will assume that the time stamp word is in the DX register: mov ax,dx shr ax,5 and al,00111111b ; (leading zeros optional) mov bminutes,al ; save in variable 5