0
/len = length, hei = height
/get and store input
input
store LEN
input 
store HEI
/start of loop
/load in the value of len
LOOP, load LEN
/add len to num
add NUM
/store the value in num
Store NUM
/load in hei deduct 1 and store back to hei
load HEI
subt ONE
store HEI
/skip jump if the value in the ac is 0 
skipcond 400
jump LOOP
/output
LOOP1, load NUM
subt TWO
store NUM
load NUM
output
skipcond 400
jump LOOP1
load NUM
/end the program
halt
LEN, DEC 0
HEI, DEC 0
ONE, DEC 1
NUM, DEC 0
TWO, DEC 2

I am attempting to make a program that calculates the area of a triangle. I've managed to get the multiplication part out of the way, however the division part is where I'm struggling. Any help would be appreciated.

I have attempted to make a second loop in which the final product would continually be subtracted by 2, however the output would just subtract 2 multiple times without getting the correct answer. For example: `

/len = 3, heigh = 4
/output would be:
/10
/8
/6
/4
/2
/0

Like I said before, any help would be appreciated.

asked Jan 1, 2024 at 19:17
3
  • When you're dividing by two, you have to keep track of the number of subtractions you do in order to get the quotient. You aren't doing that. Also, your code will only work if the result of the multiply is even. Commented Jan 1, 2024 at 19:25
  • You don't need to think in assembly language to do division by repetitive subtraction. Try it in C or a language you know well (will only be 2-3 lines of code), and then take that to assembly. Commented Jan 2, 2024 at 0:43
  • You're computing only the remainder but not the quotient. (Though there is a logic error in condition of termination for that). Commented Jan 2, 2024 at 22:42

1 Answer 1

1

Change your loop to track and return the quotient

LOOP1, load NUM
subt TWO
store NUM
load QUO <--
add ONE <--
store QUO <--
load NUM
output
skipcond 400
jump LOOP1
load QUO <--
/end the program
halt

and add:

QUO, DEC 0
answered Jan 1, 2024 at 19:38
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.