You are on page 1of 8

Assignment 2 Part 2

Solution
1. Write a MARIE program that will find the sum of the multiples of 5 between 5 and 500
inclusive, i.e. write a MARIE program to compute and output this sum

5 + 10 + 15 + . . . 495 + 500.

Loop1, LOAD Num1 /Load the first number (5)


into the accumulator
SUBT LNum /Subtract the last number
(500) from the accumulator
SKIPCOND 400 /Skip the next instruction
if the accumulator is
negative
JUMP Loop2 /If the accumulator is
negative, jump to the
second loop
LOAD Sum /Load the sum into the
accumulator
OUTPUT /Output the sum to the
console
HALT /Terminate the program

Loop2, LOAD Num1 /Load the current number


into the accumulator
ADD Five /Add 5 to the current
number
STORE Num1 /Store the new current
number back into memory
ADD Sum /Add the current number to
the sum
STORE Sum /Store the updated sum back
into memory
JUMP Loop1 /Jump back to the first
loop
Five, DEC 5 /The value 5, used in the
second loop
Sum, DEC 5 /The variable to hold the
sum of the arithmetic
sequence
Num1, DEC 5 /The variable to hold the
current number in the
second loop
LNum, DEC 500 /The value 500, used in the
first loop

Screenshot:

Explanation:

This program is an implementation of an arithmetic sequence that starts with the number 5 and

ends with the number 500.The program defines four variables: Num1, Sum, Five, and LNum.

Num1 is the variable that holds the current number in the second loop and is initialized to 5. Sum

is the variable that holds the sum of the sequence and is initialized to 5 as well, as the sequence

starts with 5. Five is a variable that holds the value 5, which is the increment added to each

number in the sequence. LNum is a variable that holds the value 500, which is the last number in

the sequence. The program uses two loops. The first loop (Loop1) starts by loading the Num1 into

the accumulator, subtracting LNum from it, and then checking if the result is negative. If the

result is negative, the program jumps to the second loop (Loop2). If the result is not negative, the

program loads the Sum variable into the accumulator and outputs it to the console before
terminating. The second loop (Loop2) starts by loading the current number in the sequence

(Num1) into the accumulator. It then adds Five to the current number, stores the new current

number back into memory, adds the current number to the Sum, stores the updated Sum back into

memory, and then jumps back to the first loop (Loop1) to continue the calculation. This loop

keeps running until the first loop detects that the sum has exceeded the last number in the

sequence.

Q2: Write a MARIE program to display ASCII (American Standard Code for Information

Interchange) on the console

loop, LOADI msg /load the start address (index

value) into accumlator

SKIPCOND 400 /check if the AC=0

JUMP out /jump to label out to

display character HALT /terminate

the program

out, OUTPUT /label- out has started: display the

output from loop LOAD msg /load msg again

ADD one /increments msg (000A) by 1 = OOOB for

first time & so on STORE msg /store the

incremented value at msg (000A)

JUMP loop

one, HEX 1

msg, hex 00B /start address

or index value DEC 65 /A


DEC 83 /S

DEC 67 /C

DEC 73 /I

DEC 73 /I

DEC 32 /
space
DEC 40 /(
DEC 65 /A

DEC 109 /m

DEC 101 /e

DEC 114 /r

DEC 105 /i

DEC 99 /c

DEC 97 /a

DEC 110 /n

DEC 32 /
space
DEC 83 /S

DEC 116 /t

DEC 97 /a

DEC 110 /n

DEC 100 /d

DEC 97 /a

DEC 114 /r

DEC 100 /d

DEC 32 /
space
DEC 67 /c

DEC 111 /o

DEC 100 /d

DEC 101 /e

DEC 32 /
space

DEC 102 /f

DEC 111 /o

DEC 114 /r

DEC 32 /
space

DEC 73 /I

DEC 110 /n

DEC 102 /f

DEC 111 /o

DEC 114 /r

DEC 109 /m

DEC 97 /a

DEC 116 /t

DEC 105 /i

DEC 111 /o

DEC 110 /n

DEC 32 /
space

DEC 73 /I
DEC 110 /n

DEC 116 /t

DEC 101 /e

DEC 114 /r

DEC 99 /c

DEC 104 /h

DEC 97 /a

DEC 110 /n

DEC 103 /g

DEC 101 /e

DEC 41 /)

DEC 000 /end of msg, it will make


AC=0

Screenshot:

Explanation:

The program is written in assembly language and is designed to display a message on the

screen. The message is stored in a variable called msg, which contains the ASCII codes for the

characters to be displayed. The character codes are stored in decimal format using the DEC
instruction.

The program starts by defining a label called loop, which is used to loop through the message

character by character. Inside the loop, the program loads the start address of the msg variable

into the accumulator using the LOADI instruction. It then uses the SKIPCOND 400 instruction

to check if the accumulator is zero. If the accumulator is not zero, it jumps to the out label to
display the character on the screen using the OUTPUT instruction. If the accumulator is

zero, the program terminates using the HALT instruction.

The out label starts with the OUTPUT instruction to display the character on the screen. Then,

it loads the msg variable again using the LOAD instruction. It increments the value of msg by

adding the value stored in the one variable (which is the value 1) using the ADD instruction.

The incremented value is then stored back in the msg variable using the STORE instruction.

Finally, it jumps back to the loop label to check if there are more characters to be displayed.

The program continues to loop through the loop and out labels until it reaches the end of the

message, which is indicated by the value 0 stored in the msg variable. The program

increments the value of msg by 1 each time it goes through the loop, which moves to the next

character in the message.

In summary, the program uses a loop to display a message character by character on the

screen. It loads the character codes from a variable, displays them on the screen, increments

the variable to move to the next character, and continues until it reaches the end of the

message.

You might also like