What to do: 1 Initialize x to be zero 2 Prompt the user for the next operation (
ID: 3620956 • Letter: W
Question
What to do:
1 Initialize x to be zero
2 Prompt the user for the next operation (Addition or Subtraction)
3 Prompt the user for the next operand (y).
4 Perform the operation (x + y or x – y depends on the operation selected by
the user), and store back the result to x.
5 Output x in decimal.
6 Go back to step 2 until user input “q” as the next operation, then exit.
• Operations:
• Addition: perform x = x + y (when user input “+”)
• Subtraction: perform x = x - y (when user input “-”)
• quit: (when user input “q”).
• Operands:
• One operand (x) is initialized to be zero (defined in .data), and updated as
the result; the other operand is always provided by the user
• Output:
• Decimal value of the current result (x).
• Extra credit:
• Include integer multiplication (*) and division (/) operations
-perform x = x * y (when user input “*”), and output the product.
- perform x = x / y (when user input “/”), and output both the
quotient and remainder. In this case, x needs to be updated as the
quotient.
Explanation / Answer
State word State0
StateMach proc
jmp State
; If this is state 0, add BX to AX and switch to state 1:
State0: add ax, bx ;addition
mov x, ax ;result store in x
mov State, offset State1 ;Set it to state 1
ret
; If this is state 1, subtract BX from AX and switch to state 2
State1: sub ax, bx ; subtract
mov x, ax ;result store in x
mov State, offset State2 ;Switch to State 2
ret
; If this is state 2, multiply AX and BX and switch to state 3:
State2: push dx
mul bx
pop dx
mov x, dx ;result store in x
mov State, offset State3 ;Switch to State 3
ret
; If in State 3, do the division and switch back to State 0:
State3: push dx
xor dx, dx ;Zero extend AX into DX.
div bx
pop dx
mov x, dx ;result store in x
mov State, offset State4 ;Switch to State 0
ret
state4: hlt ;Exit
StateMach endp
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.