Lab 4: Basic Instructions and Addressing Modes

Similar documents
Lab 6: Conditional Processing

Lab 3: Defining Data and Symbolic Constants

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

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

Lab 5: Input/Output using a Library of Procedures

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

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 8. Conditional Processing

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

Lab 2: Introduction to Assembly Language Programming

Assembly Language LAB

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

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

COMP211 ASSEMBLY PROGRAMMING

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 4: Data Transfers, Addressing, and Arithmetic. Chapter Overview

The x86 Architecture

CS 16: Assembly Language Programming for the IBM PC and Compatibles

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

Microprocessors ( ) Fall 2010/2011 Lecture Notes # 15. Stack Operations. 10 top

Assembly Language Programming

Assembly Language Lab # 4 Data Transfer & Arithmetic (1)

Data Transfer, Addressing and Arithmetic

Assembly Language. Lecture 4 Data Transfers, Addressing, and Arithmetic

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

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

Module 3 Instruction Set Architecture (ISA)

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

Assembly Language for Intel-Based Computers, 5 th Edition. Kip Irvine. Chapter 4: Data Transfers, Addressing, and Arithmetic

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

Introduction to IA-32. Jo, Heeseung

Complex Instruction Set Computer (CISC)

INTRODUCTION TO IA-32. Jo, Heeseung

The Instruction Set. Chapter 5

Instructions moving data

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures

Assembly Language for Intel-Based Computers, 5 th Edition. Chapter 4: Data Transfers, Addressing, and Arithmetic

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

3.1 DATA MOVEMENT INSTRUCTIONS 45

ADDRESSING MODES. Operands specify the data to be used by an instruction

Data Transfers, Addressing, and Arithmetic. Part 2

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Assembly Language for Intel-Based Computers, 4 th Edition. Kip R. Irvine. Chapter 4: Data Transfers, Addressing, and Arithmetic

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

COMPUTER ENGINEERING DEPARTMENT

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

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

Constants and. Lecture 7: Assembly Language Programs. Expressions (cont.) Constants and. Statements. Expressions

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

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

Basic Execution Environment

Conditional Processing

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

Defining and Using Simple Data Types

Lab 3. The Art of Assembly Language (II)

Constants and Expressions. Lecture 7: Assembly Language Programs. Constants and Expressions (cont.) Statements. Names. Assembly Directives

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

Practical Malware Analysis

Assembly Language Lab # 9

Basic Pentium Instructions. October 18

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

Chapter 11. Addressing Modes

Registers. Ray Seyfarth. September 8, Bit Intel Assembly Language c 2011 Ray Seyfarth

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

Code segment Stack segment

CS241 Computer Organization Spring 2015 IA

Addressing Modes on the x86

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

Assembly Language: IA-32 Instructions

Assembler Programming. Lecture 2

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

Computer Science Final Examination Wednesday December 13 th 2006

Philadelphia University Student Name: Student Number:

CS61 Section Solutions 3

8088/8086 Programming Integer Instructions and Computations

COMPUTER ENGINEERING DEPARTMENT

Intel 8086: Instruction Set

Assembly Language. Lecture 5 Procedures

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 3: Assembly Language Fundamentals

Program Exploitation Intro

Experiment 8 8 Subroutine Handling Instructions and Macros

Libraries and Procedures

Lecture 2 Assembly Language

EECE416 :Microcomputer Fundamentals and Design. X86 Assembly Programming Part 1. Dr. Charles Kim

Machine and Assembly Language Principles

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

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.

16.317: Microprocessor Systems Design I Fall 2013

Chapter 3: Addressing Modes

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

Computer Architecture and Assembly Language. Practical Session 3

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

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 5: Procedures. Chapter Overview. The Book's Link Library

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

LAB 5 Arithmetic Operations Simple Calculator

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

16.317: Microprocessor Systems Design I Spring 2014

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

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

Assembly Language for Intel-Based Computers, 5 th Edition

An Introduction to x86 ASM

Transcription:

COE 205 Lab Manual Lab 4: Basic Instructions and Addressing Modes - page 36 Lab 4: Basic Instructions and Addressing Modes Contents 4.1. Data Transfer Instructions 4.2. Addition and Subtraction 4.3. Data Addressing Modes 4.4. LOOP Instruction 4.5. Copying a String 4.6. Summing an Array of Integers 4.1 Data Transfer Instructions Data transfer instructions move data between registers or between registers and memory. These instructions are briefly described below. For more details, refer to the lecture notes or your textbook. The following program demonstrates the MOV, MOVZX, MOVSX, and XCHG instructions: MOV destination, source Move source to destination. MOVZX destination, source Move source to destination with zero extension. MOVSX destination, source Move source to destination with sign extension. XCHG destination, source Exchange source with destination. TITLE Data Transfer Examples (File: moves.asm) ; Demonstration of MOV, MOVZX, MOVSX, and XCHG.686.MODEL flat, stdcall.stack INCLUDE Irvine32.inc.data var1 WORD 1000h var2 WORD 2000h.code main PROC ; Demonstrating MOV and MOVZX mov ax, 0A69Bh movzx bx, al movzx ecx,ah movzx edx,ax ; Demonstrating MOVSX movsx bx, al movsx ecx,ah movsx edx,ax ; Demonstrating XCHG xchg ax,var1 xchg ax,var2 xchg ax,var1 exit main ENDP END main

COE 205 Lab Manual Lab 4: Basic Instructions and Addressing Modes - page 37 4.1.1 Lab Work: Assemble and Link moves.asm Open file moves.asm and assemble and link the file. You can use the make32 batch file from the command prompt or from the ConTEXT editor s Tools menu. 4.1.2 Lab Work: Trace the Execution of Program moves.exe Now run the Windows debugger to trace the execution of the above program. You may open the debugger from ConTEXT Tools menu or by typing: windbg QY G moves.exe Open the Watch window to view the var1 and var2 variables. You can also watch registers under the Watch window by typing their names preceded with the @ symbol as shown below. Observe that the type of registers is unsigned int and the value is shown in hexadecimal. You can change the type of a register to char, short, or int (depending on its size), to see its value as a signed decimal. Try first to guess the values of the registers and memory variables in the above program after the execution of each instruction. Write your answers in hexadecimal in the specified boxes. Place the cursor at the beginning of the main procedure and press F7. Now step through the program by pressing F10 and watch the changes in registers and variables. Make the necessary corrections to your answers. MOV and MOVZX 1) al, ah (hex) = 2) bx (hex) = 3) ecx (hex) = 4) edx (hex) = MOVSX 5) bx (hex) = 6) ecx (hex) = 7) edx (hex) = XCHG 8) ax (hex) = 8) var1 (hex) = 9) ax (hex) = 9) var2 (hex) = 10) ax (hex) = 10) var1 (hex) =

COE 205 Lab Manual Lab 4: Basic Instructions and Addressing Modes - page 38 4.2 Addition and Subtraction Integer addition and subtraction are two of the most fundamental operations that a processor can perform. In the following Program, you will learn about the INC, DEC, ADD, SUB, and NEG instructions. You will also learn about the flags affected by these arithmetic instructions: INC destination Increment destination by 1. DEC destination Decrement destination by 1. ADD destination, source Add source to destination. SUB destination, source Subtract source from destination. NEG destination Negate destination by computing 2's complement. CF Carry Flag: set if unsigned result is out of range. OF Overflow Flag: set if signed result is out of range. SF Sign Flag: set if sign bit of result is negative (or 1). ZF Zero Flag: set if result is zero (all bits are zero). PF Parity Flag: set if low byte of result has even number of 1 s. The ADD, SUB, and NEG instructions affect all the above flags. The INC and DEC instructions also affect the above flags, except that the carry flag is not modified. For more details about these instructions and flags, refer to the lecture notes or your textbook. TITLE Simple Arithmetic.686.MODEL flat, stdcall.stack INCLUDE Irvine32.inc.data ; No data (SimpleArith.asm).code main PROC ; ADD mov eax, 91ab0748h mov ebx, 3f54f8f2h add eax, ebx ; SUB mov eax, 91ab0748h sub eax, ebx ; NEG mov eax, 91ab0748h neg eax ; INC clc ; clear carry flag to show that it is not affected mov eax,7fffffffh inc eax ; DEC mov eax,0 dec eax exit main ENDP END main 4.2.1 Lab Work: Assemble and Link SimpleArith.asm

COE 205 Lab Manual Lab 4: Basic Instructions and Addressing Modes - page 39 4.2.2 Lab Work: Trace the Execution of SimpleArith.exe First, guess the values of the eax register (in hexadecimal) and the cf, of, sf, zf, and pf flags after executing the add, sub, neg, inc, and dec instructions. Write these values below: 1) EAX after ADD (hex) = CF= OF= SF= ZF= PF= 2) EAX after SUB (hex) = CF= OF= SF= ZF= PF= 3) EAX after NEG (hex) = CF= OF= SF= ZF= PF= 4) EAX after INC (hex) = CF= OF= SF= ZF= PF= 5) EAX after DEC (hex) = CF= OF= SF= ZF= PF= Run the 32-bit Windows Debugger. Open the source file SimpleArith.asm from the File menu if it is not already opened. Watch the registers by selecting Registers from the View menu. Have the registers eax, ebx and the flags cf, of, sf, zf, and pf on top of the list. Place the cursor at the beginning of main procedure and press F7 to start debugging it. Press F10 to step through the execution of the program. Watch the changes in the registers and flags. Check the answers that you have guessed above. Make the necessary corrections and try to understand why your previous answer was incorrect. 4.3 Data Addressing Modes The assembly language instructions require the specification of the location of data for source and destination operands. The specification of the location of data is called the data addressing mode. It can be classified as shown in the following diagram: Addressing Modes Register Memory Immediate Direct Indirect Register Indirect Based Indexed Based- Indexed

COE 205 Lab Manual Lab 4: Basic Instructions and Addressing Modes - page 40 Register addressing is when a register is used to specify the source or destination of an operand. This is the most efficient addressing mode because registers are implemented inside the processor and their access is very fast. Immediate addressing is when an immediate value (a constant) is used for a source operand. It cannot be used to specify a destination operand. The immediate constant is part of the instruction itself. Memory addressing is used to specify the address of the source and destination operands located in memory. This is the most detailed and interesting addressing mode. It can be divided into direct and indirect memory addressing. Direct memory addressing is when the address of a memory operand is specified directly by name. For example: mov sum, eax ; sum is a variable in memory Direct memory addressing is useful for accessing simple variables in memory, but it is useless for addressing arrays or data structures. To address the elements of an array, we need to use a register as a pointer to the array elements. This is called indirect memory addressing. It can be further classified into register indirect, based, indexed, and basedindexed, depending on how the address of the memory operand is specified. In general, a memory address can be specified as follows: Address = [BaseReg + IndexReg * Scale + Disp] This is the most general indirect memory addressing called based-indexed, because it combines a base register with an index register and a displacement. The other indirect memory addressing modes: register indirect, based, and indexed are simpler. Register Indirect Addressing: Address = [Reg] Based Addressing: Address = [BaseReg + Disp] Indexed Addressing: Address = [IndexReg * Scale + Disp] The IA-32 processor architecture supports 32-bit addressing as well as 16-bit addressing. 16- bit addressing modes originated in the 8086 processor, which supports only 16-bit registers. The 16-bit addressing modes use BX and BP for the base register and SI and DI for the index register. No scale factor is allowed, and displacements are limited to 16 bits. With the advent of the IA-32 processor architecture, registers were extended from 16 bits to 32 bits. This enabled 32-bit addressing modes in addition to 16-bit addressing. Any 32-bit general-purpose register (EAX, EBX, ECX, EDX, ESI, EDI, EBP, or ESP) can be used as a base register. Any general-purpose register, with the exception of ESP can be used as an index register. A scale factor of 1, 2, 4, or 8 is added to indexed-addressing, and displacements are extended to 32-bits. The differences between 16-bit and 32-bit addressing modes are summarized below: 16-bit Addressing 32-bit Addressing Base Register BX, BP EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP Index Register SI, DI EAX, EBX, ECX, EDX, ESI, EDI, EBP Scale Factor None 1, 2, 4, 8 Displacement 0, 8, 16 bits 0, 8, 32 bits

COE 205 Lab Manual Lab 4: Basic Instructions and Addressing Modes - page 41 4.3.1 Examples on 32-bit Addressing Modes The following program demonstrates 32-bit memory addressing modes and the LEA (Load Effective Address) instruction. The LEA instruction moves the address, rather than the value, of a source operand into destination. LEA destination, source Load Effective Address of source into destination This is similar to moving the OFFSET of a variable. For example, lea eax, variable is simlar to mov eax, OFFSET variable The LEA instruction provides more flexibility in terms of computing complex addresses at runtime. However, the OFFSET operator is used to determine the address of named variables at assembly-time. TITLE Memory Addressing Examples.686.MODEL flat, stdcall.stack INCLUDE Irvine32.inc.data arrayb BYTE "COE 205",0 arrayw WORD 100h,200h,300h, 400h arrayd DWORD 01234567h,89ABCDEFh (File: addressing.asm).code main PROC ; Direct Memory Addressing mov al, arrayb mov ah, arrayb[5] mov bx, arrayw[2] mov ecx,[arrayd] mov edx,[arrayd+2] ; same as [arrayb] ; same as [arrayb+5] ; same as [arrayw+2] ; same as arrayd ; same as arrayd[2] ; Register Indirect Addressing mov ecx, OFFSET arrayb + 3 mov edx, OFFSET arrayw + 1 mov bx, [ecx] ; address in [ecx] mov al, [edx] ; address in [edx] ; Based Addressing mov edx, 4 mov al, arrayb[edx] mov bx, arrayw[edx] mov ecx,arrayd[edx] ; Scaled Indexed Addressing mov esi, 1 mov arrayb[esi*2], 'S' mov arrayw[esi*2], 102h mov arrayd[esi*4], 0 ; Load Effective Address (LEA) lea eax, arrayb lea ebx,[eax + LENGTHOF arrayb] lea ecx,[ebx + esi*8] lea edx, arrayd exit main ENDP END main

COE 205 Lab Manual Lab 4: Basic Instructions and Addressing Modes - page 42 4.3.2 Lab Work: Assemble and Link addressing.asm 4.3.3 Lab Work: Trace the Execution of Program addressing.exe First, guess the values of the registers and memory variables in program addressing.asm. Write your answers in hexadecimal in the specified boxes after the execution of each instruction. For characters, show the character symbol as well as its code in hexadecimal. Run the Windows Debugger. Open the source file addressing.asm from the File menu if it is not already opened. Watch the registers and memory by selecting them in the View menu. In the Memory window, write the name of the first variable arrayb in the Virtual address box. You may resize the Memory window so that exactly 16 bytes are displayed on each line. Place the cursor at the beginning of main procedure and press F7. Press F10 to step through the execution of the program. Watch the changes in the registers and memory. Direct Memory Addressing 1) al = 2) ah = 3) bx = 4) ecx = 5) edx = Register Indirect Addressing 6) ecx = 7) edx = 8) bx = 9) al = Based Addressing 11) al = 12) bx = 13) ecx = Scaled Indexed Addressing show the address and byte values in memory 15) arrayb address byte byte byte byte byte byte byte byte 16) arrayw address byte byte byte byte byte byte byte byte 17) arrayd address byte byte byte byte byte byte byte byte Load Effective Address (LEA) 18) eax = 19) ebx = 20) ecx = 21) edx =

COE 205 Lab Manual Lab 4: Basic Instructions and Addressing Modes - page 43 4.4 LOOP Instruction The LOOP instruction provides a simple way to repeat a block of statements a specific number of times. It uses ECX as a counter, which is decremented each time the loop repeats. The execution of the LOOP instruction involves two steps: First, it subtracts 1 from ECX. If ECX is not equal to zero, a jump is taken to the label (start of the loop). Otherwise, the loop terminates and the next instruction is executed. LOOP label Decrement ECX and Jump to label if ECX 0 4.5 Copying a String Study the following program: TITLE Copying a String (File: CopyStr.asm) ; Demonstrates LOOP instruction and array indexing.686.model flat, stdcall.stack INCLUDE Irvine32.inc.data source BYTE "This is the source string",0 target BYTE SIZEOF source DUP(0).code main PROC mov esi, 0 mov ecx, SIZEOF source L1: mov al, source[esi] mov target[esi], al inc esi loop L1 ; used to index source and target ; loop counter ; get a character from source ; store it in the target ; increment index ; repeat for entire string exit main ENDP END main What is the value of the target string in memory after finishing the execution of loop L1?... What is the number of iterations for loop L1?... 4.5.1 Lab Work: Assemble and Link CopyStr.asm 4.5.2 Lab Work: Trace the Execution of CopyStr.exe Run the 32-bit Windows Debugger. View the registers esi, ecx, and al, as well as the source and target variables in memory. You can open two Memory windows to view separately the source and target strings. Put the cursor at the beginning of main procedure and press F7 to start debugging it. Press F8 to trace the execution of the loop, iteration by iteration. If you press F10 on the LOOP instruction, it will execute ALL the loop iterations and will terminate the loop. This is why it

COE 205 Lab Manual Lab 4: Basic Instructions and Addressing Modes - page 44 is better here to use F8 to trace the execution of the loop. View how the esi, ecx, and al registers change, as well as memory for the target variable. Check your previous answers, make the necessary corrections, and try to understand your mistakes. 4.6 Summing an Array of Integers Program SumArray.asm uses register esi as a pointer to intarray. Register-indirect addressing is used to access the elements of intarray. TITLE Summing an Array of Signed Words (File: SumArray.asm) ; Register-Indirect memory addressing is used.686.model flat, stdcall.stack INCLUDE Irvine32.inc.data intarray SWORD 5,7,-3,100,0,-9, 10 DUP(-999) sum SWORD?.code main PROC L1: mov esi,offset intarray ; esi = pointer to intarray mov ecx,lengthof intarray ; ecx = loop counter mov ax,0 ; zero the accumulator add ax,[esi] ; register esi is a pointer add esi,type intarray ; point to next integer loop L1 ; repeat until ECX = 0 mov exit main ENDP END main sum, ax What is the value of sum (in decimal) at the end of the program?... How many iterations was loop L1 repeated?... Assuming the initial value of ESI is 404000h before starting loop L1, what is the final value of ESI (in hex) after finishing loop L1?... 4.6.1 Lab Work: Assemble, Link, and Trace Program Execution Open file SumArray.asm and assemble and link the file. Now run the Windows debugger to trace the execution of the above program. You need to view the registers esi, ecx, and ax. Add also a watch for the sum variable. Put the cursor at the beginning of main procedure and press F7 to start debugging this procedure. Press F8 to trace the execution of the loop, iteration by iteration. View how the esi, ecx, and ax registers change. Press F10 to exit the program. Check your answers and make the necessary corrections.

COE 205 Lab Manual Lab 4: Basic Instructions and Addressing Modes - page 45 Review Questions 1. For each of the following, write the destination register value (in hexadecimal) if the instruction is valid. Otherwise, indicate that the instruction is invalid. Assume that var1 is at virtual address 404000h. var1 SBYTE -4, 2 var2 WORD 1000h, 2000h, 3000h var3 DWORD 1, 2, 3, 4, 5 a. mov ax, var1... b. movzx ax, var1... c. movsx eax, var1... d. mov ax, var2[2]... e. mov bx, var3... f. mov edx, [var3+4]... g. lea esi, var2... h. mov al, [esi]... i. mov ax, [esi]... j. mov eax, [esi]... k. inc [esi]... 2. (Yes/No) Is it possible to set the Overflow flag if you add a positive to a negative integer? 3. (Yes/No) Is it possible for the NEG instruction to set the Overflow flag? 4. (Yes/No) Is it possible for both the Sign and Zero flags to be set at the same time? 5. (Yes/No) Can any 16-bit general-purpose register be used for indirect addressing? 6. (Yes/No) Can any 32-bit general-purpose register be used for indirect addressing? Programming Exercises 1. Write a program that does the following: Use the ADD and SUB instructions to set and clear the Carry flag. Use the ADD and SUB instructions to set and clear the Zero and Sign flags. Use the ADD and SUB instruction to set and clear the Overflow flag. Use the ADD instruction to set and clear both the Carry and Overflow flags. Trace program execution and Explain why the flags are affected by each instruction. 2. Write a program that uses a loop to calculate the first ten values in the Fibonacci number sequence {1, 1, 2, 3, 5, 8, 13, 21, 34, 55}. Place each value in the EAX register inside the loop, and trace it with the Windows debugger. 3. Modify Program SumArray.asm to use the register esi as an index to intarray with a scale factor of 2, instead of a pointer. Trace it with the Windows debugger. 4. Modify Program CopyStr.asm to use the register esi as a pointer (indirect addressing), instead of an index, to copy the characters from source to target, but in reverse order.