Friday, June 8, 2012

[Mips] Decimal to Roman Numeral

This is mips code that changes decimal to roman numeral.
main:
la  $a0, enter2 # prompt user with "Please enter an integer in the range [1,100]."
li  $v0, 4
syscall
li  $v0, 5      # read user input, j
syscall
blez    $v0, error  # if $v0 equal or less than 0 then print error msg. then jump to main.
bge $v0, 101, error # if $v0 equal or greater than 101 then print error msg. then jump to main.
move    $t0, $v0    # Save $v0 value to $t0
loop1:  
blez    $t0, done   # if $t0 equal or less than 0 
bge     $t0, 100, hund  # if $t0 equal 100 then print "C" and exit. 
bge $t0, 90, ninty  # if $t0 equal or greater then 90 then print "XC" and sub $t0-90
bge $t0, 60, sixty  # if $t0 equal or greater then 60 then print "LX" and sub $t0-60
bge $t0, 50, fifty  # if $t0 equal or greater then 50 then print "L" and sub $t0-50
bge $t0, 40, forty  # if $t0 equal or greater then 40 then print "XL" and sub $t0-40
bge $t0, 10, ten    # if $t0 equal or greater then 10 then print "X" and sub $t0-10
bge $t0, 9, nine    # if $t0 equal or greater then 9 then print "IX" and sub $t0-9
bge $t0, 5, five    # if $t0 equal or greater then 5 then print "V" and sub $t0-5
bge $t0, 4, four    # if $t0 equal or greater then 4 then print "IV" and sub $t0-4
bge $t0, 1, one # if $t0 equal or greater then 1 then print "I" and sub $t0-1
hund:   
la  $a0, C          
li  $v0, 4
syscall
j   done
ninty:  
la  $a0, XC
li  $v0, 4
syscall
addi    $t0,$t0,-90 
b   loop1
sixty:  
la  $a0, LX
li  $v0, 4
syscall 
addi    $t0,$t0,-60
b   loop1
fifty:  
la  $a0, L
li  $v0, 4
syscall
addi    $t0,$t0,-50
b   loop1
forty:  
la  $a0, XL
li  $v0, 4
syscall
addi    $t0,$t0,-40
b   loop1
ten:    
la  $a0, X
li  $v0, 4
syscall
addi    $t0,$t0,-10
b   loop1
nine:   
la  $a0, IX
li  $v0, 4
syscall
addi    $t0,$t0,-9
b   loop1
five:   
la  $a0, V
li  $v0, 4
syscall
addi    $t0,$t0,-5  
b   loop1
four:   
la  $a0, IV
li  $v0, 4
syscall
addi    $t0,$t0,-4
b   loop1
one:    
la  $a0, I
li  $v0, 4
syscall
addi    $t0,$t0,-1
b   loop1
error:  
la  $a0, enter1     # Print error meg. then back to main
li  $v0, 4
syscall
j   main
done:   
li  $v0, 10         # Exit
syscall 

.data
enter1: .asciiz "Number out of range.Please enter another number\n"
enter2: .asciiz "Please enter an integer in the range [1,100]. "
debug:  .asciiz "reach"
C:  .asciiz "C"
XC: .asciiz "XC"
LX: .asciiz "LX"
L:  .asciiz "L"
XL: .asciiz "XL"
X:  .asciiz "X"
IX: .asciiz "IX"
V:  .asciiz "V"
IV: .asciiz "IV"
I:  .asciiz "I"


output will be something like this...
Please enter an integer in the range [1,100]. 15
XV

No comments: