In: Computer Science
Lab-3A One-way Conditional Branching
Write MIPS assembly code to prompt and read an integer value X, and to calculate and display |X|.
Example Input/Output
Enter an integer X : 14
The absolute value is 14
Enter an integer X: -14
The absolute value is 14
Submit a single word document with the:
~MIPS code and screenshots showing:
~ ML code & Registers before execution
~ Input/Output console with at least three test data
(should cover all partitions – one positive number, one negative number, and zero)
Screenshot(Program)




Program
###########################################################
#Create a program to find absolute value
#Of user entered value
###########################################################
.data
input: .asciiz "Enter an integer X : "
output: .asciiz "The absolute value is "
#Program starts here
.globl main
.text
main:
#Prompt for input
addi $v0,$0,4
la $a0,input
syscall
#Read input
addi $v0,$0,5
syscall
#Store into a0
add $a0,$0,$v0
#Call function to get absolute value
jal absolute
#Result in a0
add $t0,$0,$v0
#Display output message
addi $v0,$0,4
la $a0,output
syscall
#Display result
add $a0,$0,$t0
addi $v0,$0,1
syscall
#Display next line
addi $a0,$0,10
addi $v0,$0,11
syscall
#Prompt for input
addi $v0,$0,4
la $a0,input
syscall
#Read input
addi $v0,$0,5
syscall
#Store into a0
add $a0,$0,$v0
#Call function to get absolute value
jal absolute
#Result in a0
add $t0,$0,$v0
#Display output message
addi $v0,$0,4
la $a0,output
syscall
#Display result
add $a0,$0,$t0
addi $v0,$0,1
syscall
#End of the program
addi $v0,$0,10
syscall
#Implementation of absolute value
absolute:
#number in v0
add $v0,$0,$a0
slt $t0,$0,$v0
beq $t0,0,negation
jr $ra
#Convert to positive
negation:
sub $v0,$0,$v0
jr $ra
Output
