Skip to content

Commit

Permalink
Merge pull request MARIE-js#287 from kishorliv/fix-multiply-example
Browse files Browse the repository at this point in the history
(MARIE-js#286) Fix multiplication when the second input(Y) is negative.
  • Loading branch information
auroranil authored Jun 9, 2020
2 parents 2723f0e + 104b47e commit 20c6503
Showing 1 changed file with 46 additions and 9 deletions.
55 changes: 46 additions & 9 deletions src/code/multiply.mas
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,67 @@
/ Copyright (C) 2016. Licensed under the MIT License

/ Prompt user to type in integers
INPUT
Clear
Store result
Input
Store X
INPUT
Input
Store Y

/ check if Y is negative, if -ve negate Y and set negative flag
Load Y
Skipcond 000
Jump nonneg

Subt Y
Subt Y
Store Y
Clear
Add one
Store negflag
Clear
Jump loop

nonneg, Clear
Store negflag
/ check if Y is zero, if it is, then we jump to halt
Load Y
Skipcond 400
Jump loop / false
Jump halt / true

/ Loop for performing iterative addition
loop, Load num
loop, Load result
Add X
Store num
Store result

Load Y
Subt one
Store Y

Skipcond 400 / have we completed the multiplication?
Jump loop / no; repeat loop
/ yes, so exit the loop

/ check for negative flag, if it is set, negate the result
Load negflag
Skipcond 800
Jump halt

/ negate result
Load result
Subt result
Subt result
Store result
/ run the next three instructions, which halts the program

/ Output result to user then halt program
Load num
Output
Halt
halt, Load result
Output
Halt

X, DEC 0
Y, DEC 0
num, DEC 0
one, DEC 1
negflag, DEC 0
result, DEC 0

0 comments on commit 20c6503

Please sign in to comment.