MOV.W Design And Write An MSP430 Assembly Language Subroutine That Returns The Minimum Absolute Value Of A Signed Integer Array, I.E., MINABS=Minimuma(I)), I=0, … N-1, N>0. The Main Program That Calls The Subroutine Is Shown Below. What Does The Main Program Do With The Returned Value? How Do We Pass The Input Parameters? Fill In The Missing Comment Fields
The Correct Answer and Explanation is:
Here’s the assembly language subroutine along with the given main program for the MSP430 that returns the minimum absolute value of a signed integer array. This includes the filled-in missing comments and a 300-word explanation afterward.
🧠 Main Program (Given with comments filled in)
.text
.global main
main:
MOV.W #array, R4 ; R4 points to the start of the array
MOV.W #5, R5 ; R5 = number of elements in the array (N = 5)
CALL #MINABS ; Call subroutine to compute min abs value
MOV.W R12, result ; Store the returned min abs value into memory at 'result'
JMP $ ; Loop forever (end of program)
array: .word -3, 7, -9, 2, -1 ; Example input array
result: .word 0 ; To store the result returned by the subroutine
🔧 Subroutine to Find Minimum Absolute Value
MINABS: PUSH R4 ; Save registers (callee-save)
PUSH R5
PUSH R6
PUSH R7
MOV.W #0FFFFh, R12 ; R12 = current minimum absolute value (initialize to max 16-bit)
MOV.W R5, R6 ; R6 = counter (number of elements)
MOV.W R4, R7 ; R7 = pointer to array
Loop: CMP #0, R6 ; Check if counter is zero
JEQ Done ; If yes, we're done
MOV.W @R7+, R9 ; Load next array element into R9, increment pointer
CMP #0, R9
JGE Positive ; If R9 >= 0, jump to Positive
MOV.W R9, R10
RLA R10 ; Multiply by 2
SUB.W R9, R10 ; R10 = -R9 (absolute value)
JMP Compare
Positive: MOV.W R9, R10 ; R10 = R9 (positive, so abs same)
Compare: CMP R10, R12
JGE Skip ; If current min <= abs value, skip
MOV.W R10, R12 ; Else, update current min abs
Skip: DEC R6 ; Decrement counter
JMP Loop ; Repeat loop
Done: POP R7
POP R6
POP R5
POP R4
RET ; Return, result in R12
✅ What the Main Program Does with the Returned Value
The main program stores the returned value (the minimum absolute value found in the array) from register R12 into a memory location labeled result. This result can be used later or inspected in memory.
Explanation
The provided MSP430 assembly subroutine MINABS finds the minimum absolute value in a signed integer array. This is useful in applications such as signal processing or error analysis where proximity to zero is important regardless of sign.
The main program first sets up the environment by loading the address of the array into register R4 and the count of elements N into register R5. These act as input parameters passed to the subroutine via registers. MSP430 uses register-based calling conventions, and no stack is needed for these arguments in simple subroutines.
Within the MINABS subroutine, R12 is used to store the currently known minimum absolute value, initialized to the maximum possible 16-bit value (0xFFFF) to ensure the first comparison always updates it.
A loop processes each element of the array. If the element is negative, it is converted to its absolute value using arithmetic (since MSP430 doesn’t have a dedicated ABS instruction). The absolute value is then compared to the current minimum. If it is smaller, it replaces the current minimum.
Once all elements are processed, the final result (minimum absolute value) is in R12. Upon return, the main program stores this in memory at the label result.
This illustrates key low-level programming concepts: pointer arithmetic, conditional branching, looping, and calling conventions—critical for understanding embedded systems.
