Friday, June 8, 2012

[Mips] Decimal to Binary and Display with 3 by 3 Metric Form.

.data
ask:    .asciiz "\nEnter a number between 0 and 511(Negative number for quit): "
errmgs: .asciiz "Number out of range.Please enter another number\n"
output: .asciiz "The number in binary base is: "
line:   .asciiz "\n"
H:  .asciiz "H"
T:  .asciiz "T"
.align 2
.text
.globl main

main:   li  $v0, 4
    la  $a0, ask
    syscall
    li  $v0, 5
    syscall
    bltz    $v0, done       # if $v0 equal or less than 0 then quit. 
    bge $v0, 511, error     # if $v0 equal or greater than 511 then print error msg. then jump to main. 
    move    $a0, $v0
    jal bin
    j   main
    
bin:    add $t0, $zero, $a0     
    add $t1, $zero, $zero   
    addi    $t3, $zero, 1       
    sll     $t3, $t3, 8     
    addi    $t4, $zero,9        
    
loop:   beq $t4, 3, newline1
loop1:  beq $t4, 6, newline2
loop2:  and     $t1, $t0, $t3       
    beq     $t1, $zero, printH  
    add     $t1, $zero, $zero   
    addi    $t1, $zero, 1       
    j   printT
    
error:  la  $a0, errmgs     # Print error meg. then back to main
    li  $v0, 4
    syscall
    j   main    

printH: la  $a0, H
    li  $v0, 4
    syscall
    srl     $t3, $t3, 1
    addi    $t4, $t4, -1
    bne $t4, $zero, loop
    jr  $ra
    
printT: la  $a0, T
    li  $v0, 4
    syscall
    srl     $t3, $t3, 1
    addi    $t4, $t4, -1
    bne $t4, $zero, loop
    jr  $ra

newline1:la $a0, line       # Print a line.
    li  $v0, 4
    syscall
    bne $t4, $zero, loop1
    jr  $ra
    
newline2:la $a0, line       # Print a line.
    li  $v0, 4
    syscall
    bne $t4, $zero, loop2
    jr  $ra

done:   li  $v0, 10         # Exit
    syscall 


Output will be...

Enter a number between 0 and 511(Negative number for quit):15
HHH
HHT
TTT
Enter a number between 0 and 511(Negative number for quit):55
HHH
TTH
TTT
Enter a number between 0 and 511(Negative number for quit):88
HHT
HTT
HHH
Enter a number between 0 and 511(Negative number for quit):-1

-- program is finished running --



No comments: