In: Computer Science
Write a program using MARIE’s assembly language. The program consists of a main program and a subroutine. It uses the subroutine to process a given number. If the value of the number is negative, the value will be replaced with zero and returned. Otherwise, the value is doubled and returned. Then, in the main program, it calls the subroutine to process for X = -3 and saves the value after the processing.
PROGRAM:
ORG 100
JNS SUBR /Store return address, jump
to procedure
HALT /End
program
SUBR, HEX 000 /Store return address here
LOAD X /Load
value of X into AC
SKIPCOND 000 /Skip next instruction if AC is
negative
JUMP NO /If AC is
>=0, Jump to label No to double value of X
Clear /Else Clear
AC
STORE X /Store AC
to X
JUMPI SUBR /Return to calling
code
NO, LOAD X /Load value of X into
AC
STORE Y /Store AC
to Y, to get multiplication by repeated addition
loop, LOAD X /Load value of X into AC
ADD multiply /Add contents of AC to contents of
multiply
STORE multiply /Store the result
LOAD Y /Load
value of Y into AC
SUBT one /Decrement Y by 1
STORE Y /Store
the decremented value
SKIPCOND 400 /Skip next instruction if AC=0
JUMP loop /Repeat the loop if AC is
not equal 0
LOAD multiply /Load the doubled value of X into
AC
STORE X /Store it
into X
JUMPI SUBR /Return to calling
code
X, DEc -3
Y, DEc 0
one, Dec 1
multiply, Dec 0
Please refer to the following screenshot of the program for indentation of the code:
OUTPUT:
Memory contents before execution:
Memory contents after execution of the code:
Contents of memory if the value of X is changed to 2 and executed the code: