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.