Lab_3
Lab_3
Lab Objective:
This lab focuses on performing arithmetic operations on data stored in memory,
specifically adding the contents of memory locations and multiplying two numbers
using techniques other than the MUL instruction (e.g., repeated addition or bit
shifting).
Exercise Code 1:
The following program demonstrates integer data definition under the .DATA
section. You may open and view this program in any text editor. Assemble and link
this program to produce the addvariables.exe executable file.
TITLE Integer Data Definitions (File:addvariables.asm)
; Examples Demonstrating Integer Data Definition
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.data
num1 word 1000h
num2 word 2000h
sum word ?
.code
main PROC
MOV AX, num1 ; Load num1 into AX
ADD AX, num2 ; Add num2 to AX
MOV sum, AX ; Store the sum in memory (sum)
exit
main ENDP
END main
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad
CSC-221 Lab Manual Lab 3: Memory Addition and Multiplication
Task:
Exercise Code 2:
TITLE Add and Subtract (multiply.asm)
; This program loads values into registers
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.DATA
multiplicand word 5
multiplier word 3
product word 0
.code
main PROC
loop_start:
ADD DX, AX ; Add multiplicand to product
INC CX ; Increment loop counter
CMP CX, BX ; Compare loop counter with multiplier
JNE loop_start ; Jump if not equal (loop again)
exit
main ENDP
END main
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad
CSC-221 Lab Manual Lab 3: Memory Addition and Multiplication
Task:
– Assemble, link, and run the code.
– Use a debugger to verify the result in product.
– Modify the values of multiplicand and multiplier and observe the
changes in product.
– Implement multiplication for byte values.
Exercise Code 3:
TITLE Add and Subtract (multiply.asm)
; This program loads values into registers
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.DATA
multiplicand word 5
multiplier word 3
product word 0
.code
main PROC
loop_start:
TEST BX, 1 ; Check if LSB of multiplier is 1
JZ no_add ; Jump if LSB is 0
ADD DX, AX ; Add multiplicand to product if LSB is 1
no_add:
SHL AX, 1 ; Shift multiplicand left by 1
SHR BX, 1 ; Shift multiplier right by 1
CMP BX, 0 ; Check if multiplier is 0
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad
CSC-221 Lab Manual Lab 3: Memory Addition and Multiplication
exit
main ENDP
END main
Task:
Dry Run:
AX BX C Flags
Step Instruction DX (Binary) Notes
(Binary) (Binary) X (ZF)
1 MOV AX, multiplicand 0000 0101 0000 0011 0000 0000 0 - AX = 5
2 MOV BX, multiplier 0000 0101 0000 0011 0000 0000 0 - BX = 3
3 MOV CX, 0 0000 0101 0000 0011 0000 0000 0 - CX = 0
4 MOV DX, 0 0000 0101 0000 0011 0000 0000 0 - DX = 0
5 TEST BX, 1 0000 0101 0000 0011 0000 0000 0 - LSB of BX is 1
6 ADD DX, AX 0000 0101 0000 0011 0000 0101 0 - DX = 5
7 SHL AX, 1 0000 1010 0000 0011 0000 0101 0 - AX = 10
8 SHR BX, 1 0000 1010 0000 0001 0000 0101 0 - BX = 1
9 CMP BX, 0 0000 1010 0000 0001 0000 0101 0 0 BX != 0
10 JNE loop_start 0000 1010 0000 0001 0000 0101 0 0 Loop again
11 TEST BX, 1 0000 1010 0000 0001 0000 0101 0 - LSB of BX is 1
12 ADD DX, AX 0000 1010 0000 0001 0000 1111 0 - DX = 15
13 SHL AX, 1 0001 0100 0000 0001 0000 1111 0 - AX = 20
14 SHR BX, 1 0001 0100 0000 0000 0000 1111 0 - BX = 0
15 CMP BX, 0 0001 0100 0000 0000 0000 1111 0 1 BX == 0
16 MOV product, DX 0001 0100 0000 0000 0000 1111 0 1 product = 15
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad
CSC-221 Lab Manual Lab 3: Memory Addition and Multiplication
Lab Questions:
Q#1. Modify the Exercise Code 2 and Exercise Code 3 and perform the same
program using LOOP instruction.
Q#2. Modify the Exercise Code 2 and Exercise Code 3 to multiply 32-bit numbers.
Q#3. Use SHIFT or ROTATE instructions to write assembly language instructions
that calculate EAX * 24 using binary multiplication.
Q#4. What are the advantages and disadvantages of using repeated addition for
multiplication?
Q#5. Explain how bit shifting and addition can be used to perform multiplication.
Q#6. How would you extend the multiplication code to handle signed numbers?
Prepared by: Asad Ullah Khan Iqra University H-9 Campus Islamabad