asked 133k views
4 votes
Build an assembly language subroutine named Normalize that will take an argument on the stack and transform it as per the following description. If the value of the argument is odd, your subroutine should just return the original argument on the stack. If the value of the argument is greater than the value in R0, your subroutine should subtract the value in R1 from the value of the argument and return the new value of the argument by placing it on the stack. If the value of the argument is less than the value in R1, your subroutine should add the value of R0 to the value of the argument and return the new value of the argument by placing it on the stack. Your subroutine must be placed at address 0x20 in program memory and must use only instructions that are contained in the CS150 AVR instruction subset. The following code shows how your subroutine will be used:

asked
User Aberna
by
9.2k points

2 Answers

2 votes

Final answer:

The 'Normalize' assembly language subroutine checks if an argument from the stack is odd, directly returning it if so. If even, it adjusts the argument based on whether it is greater than or less than R0, using R1. It carefully preserves the R0 and R1 registers during execution.

Step-by-step explanation:

To write an assembly language subroutine named Normalize, we must follow the given rules and use only the instructions available in the CS150 AVR instruction subset. The subroutine should handle an argument pushed onto the stack and determine whether to return the value as is, subtract the value in R1 or add the value in R0 based on the given conditions.

Assembly Subroutine - Normalize

Normalize:
PUSH R0 ; Save R0 on the stack
PUSH R1 ; Save R1 on the stack
POP R2; Get argument from the stack into R2
MOV R3, R2
ANDI R3, 0x01 ; Check if R2 is odd
BREQ EvenCheck
OddReturn:
PUSH R2; If odd, push the original argument back on stack
POP R1 ; Restore R1
POP R0 ; Restore R0
RET
EvenCheck:
CP R2, R0
BRLO LessThanR0
SUB R2, R1 ; If argument > R0, subtract R1
RJMP End
LessThanR0:
ADD R2, R0 ; If argument < R1, add R0
End:
PUSH R2; Push the result back on the stack
POP R1 ; Restore R1
POP R0 ; Restore R0
RET

This subroutine checks if the argument on the stack is odd and returns it directly if so. Otherwise, it compares the argument with R0 and alters the argument based on the conditionals stated, performing either an addition or a subtraction as needed. The subroutine ensures that registers R0 and R1 are preserved throughout execution.

answered
User Manan
by
8.1k points
4 votes

Final answer:

The subroutine named Normalize in assembly language takes an argument on the stack and performs conditional operations to normalize it.

Step-by-step explanation:

The subroutine named Normalize in assembly language can be built using the CS150 AVR instruction subset. The subroutine will take an argument on the stack and perform the following operations:

  1. If the argument is odd, it will return the original argument on the stack.
  2. If the argument is greater than the value in R0, it will subtract the value in R1 from the argument and return the new value on the stack.
  3. If the argument is less than the value in R1, it will add the value in R0 to the argument and return the new value on the stack.

To achieve this, you can use conditional branches and arithmetic instructions in the assembly language. The subroutine should be placed at address 0x20 in program memory.

answered
User Basia
by
8.1k points