Programming lesson
Building a Multiplication Routine in MARIE Assembly: A Step-by-Step Guide
Learn how to implement multiplication using repeated addition in MARIE assembly language for CDA3103 Project 1. This guide covers the algorithm, step-by-step coding, and testing on the MARIE simulator.
Introduction to MARIE Assembly and Multiplication
In the CDA3103 Project 1, you are tasked with writing a MARIE assembly program that computes z = a * b * c. Since MARIE does not have a multiply instruction, you must implement multiplication using repeated addition. This guide will walk you through the algorithm, code structure, and testing on the MARIE simulator. All numbers are positive integers.
Understanding the Multiplication Algorithm
Multiplication can be performed by repeated addition. For example, 4 * 3 is 4 + 4 + 4 = 12. To compute a * b, add a to itself b times. Then multiply the result by c similarly.
Step-by-Step Algorithm
- Read input
aand display it. - Read input
band display it. - Read input
cand display it. - Compute
temp = a * busing a loop that addsarepeatedlybtimes. - Compute
z = temp * cusing a similar loop. - Display the result
z.
MARIE Assembly Code Structure
Below is a template for the program. Replace Your Name with your actual name.
ORG 100
/ Program to compute z = a * b * c
/ Your Name
/ Input a, b, c; display each; output z
INPUT
STORE a
OUTPUT
INPUT
STORE b
OUTPUT
INPUT
STORE c
OUTPUT
/ Multiply a * b -> temp
LOAD a
STORE temp
LOAD b
STORE count
Loop1, SUBT one
SKIPCOND 000
JUMP EndMul1
LOAD temp
ADD a
STORE temp
LOAD count
JUMP Loop1
EndMul1, / temp = a * b
/ Multiply temp * c -> z
LOAD temp
STORE z
LOAD c
STORE count
Loop2, SUBT one
SKIPCOND 000
JUMP EndMul2
LOAD z
ADD temp
STORE z
LOAD count
JUMP Loop2
EndMul2, / Output result
LOAD z
OUTPUT
HALT
a, DEC 0
b, DEC 0
c, DEC 0
temp, DEC 0
z, DEC 0
count, DEC 0
one, DEC 1Explanation of the Code
The program uses three main sections: input, first multiplication loop, and second multiplication loop. Variables a, b, c store inputs. temp holds the intermediate product. count is a loop counter initialized to the multiplier. The one constant is used to decrement the counter.
In each multiplication loop, we subtract 1 from count and check if it becomes zero using SKIPCOND 000 (skip if AC = 0). If not zero, we add the multiplicand to the running product and repeat.
Testing on the MARIE Simulator
After writing the code in a .mas file, load it into the MARIE simulator. Assemble the code, then run step-by-step or continuously. Enter positive integers for a, b, and c. Verify that each input is displayed and the final result is correct. For example, with a=2, b=3, c=4, you should see output 2, 3, 4, 24.
Common Pitfalls and Tips
- Ensure all variables are initialized to 0 before use.
- The
SKIPCONDinstruction checks the AC value. After subtracting 1, if AC becomes 0, the next instruction is skipped. - Use
ORG 100to start the program at address 100 (typical for MARIE). - Include comments for clarity, especially for your name as required.
Trend Connection: Multiplication in AI and Gaming
Multiplication is fundamental in computer graphics, AI calculations, and even in popular games like Minecraft where coordinates are multiplied for world generation. Understanding low-level multiplication in MARIE helps you appreciate how modern CPUs handle arithmetic efficiently.
Conclusion
By following this guide, you can successfully implement multiplication in MARIE assembly for CDA3103 Project 1. Test thoroughly and ensure your .mas file includes your name as a comment. Good luck!