0% found this document useful (0 votes)
11 views

Lab_3

Lab 3 of the CSC-221 Lab Manual focuses on performing memory addition and multiplication using assembly language. It includes exercises for adding contents of memory locations, multiplying using repeated addition, and multiplying using bit shifting and addition. The lab also provides tasks for modifying code and implementing operations for different data types, along with questions for further exploration of the concepts.

Uploaded by

haxansaleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab_3

Lab 3 of the CSC-221 Lab Manual focuses on performing memory addition and multiplication using assembly language. It includes exercises for adding contents of memory locations, multiplying using repeated addition, and multiplying using bit shifting and addition. The lab also provides tasks for modifying code and implementing operations for different data types, along with questions for further exploration of the concepts.

Uploaded by

haxansaleem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CSC-221 Lab Manual Lab 3: Memory Addition and Multiplication

Lab 3: Memory Addition and Multiplication

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).

1. Adding Contents of Memory Locations:


Learn how to load data from memory, perform addition, and store the result back
into memory.

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:

– Assemble, link, and run the code.


– Use a debugger to examine the memory location of sum and verify the result.
– Modify the initial values of num1 and num2 and observe the changes in sum.
– Extend the program to add byte values instead of word values.

2. Multiplication Without the MUL Instruction (repeated


addition):
Implement multiplication using repeated addition.

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

MOV AX, multiplicand


MOV BX, multiplier
MOV CX, 0 ; Initialize loop counter
MOV DX, 0 ; Initialize product

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)

MOV product, DX ; Store the product in memory

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.

3. Multiplication Without the MUL Instruction (Bit Shifting and


Addition):
Implement multiplication using bit shifting and addition.

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

MOV AX, multiplicand


MOV BX, multiplier
MOV CX, 0 ; Initialize loop counter
MOV DX, 0 ; Initialize product

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

JNE loop_start ; Jump if not equal (loop again)

MOV product, DX ; Store the product in memory

exit
main ENDP
END main

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.

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

You might also like