简体   繁体   中英

Double to Float in mips mars assembly

So i have this code that reads an array of doubles and do the gauss elemination, and i want to change it to read floats, my question is if i change the array to float what do i need to change to my program continuing runing.

i know that my array should change to.float and in floats i need to use li $v0, 2 and lcw1 $f12, and i am using li $v0, 3 ldc1 $f2, but i keep changing it and gives me wrong answer i dont know how to do this, i am trying but i cant make it work any help plz?

### Text segment 

.eqv n 3
    .text

main:
    la  $a0, array
    li  $a1, n
    jal     print_matrix
    nop
    jal     gauss_reduct
    nop
    la  $a2, solution
    jal     gauss_solve
    nop
    jal     print_solution
    nop

exit:   
    li  $v0, 10
    syscall


gauss_reduct:
    addiu   $sp,  $sp, -24
    sw  $ra,  20($sp)
    sw  $s2,  16($sp)
    sw  $s1,  12($sp)
    sw  $s0,  8($sp) 
    sw  $a1,  4($sp) 
    sw  $a0,  0($sp)

    add $t3, $a0, $zero
    addi    $t4, $a1, -1
    addi    $t5, $a1, 0

    add $t2, $zero, $zero
gauss_reduct_ciclok:
    beq $t2, $t5, gauss_reduct_end
    nop

    add $t1, $zero, $zero
gauss_reduct_cicloj:
    beq $t1, $t5, gauss_reduct_fim_ciclo_j
    nop

    beq $t1,$t2,gauss_reduct_cicloj_continue
    nop

    move    $a0, $t1
    move    $a1, $t2
    jal fetchaddress
    nop
    move    $s1, $v0

    ldc1    $f6,($s1)

    move    $a0, $t2
    move    $a1, $t2
    jal fetchaddress
    nop
    move    $s1, $v0

    ldc1    $f8,($s1)

    div.d   $f4,$f6,$f8

    add $t0,$zero,$zero

    move    $a0, $t1
    move    $a1, $t0
    jal fetchaddress
    nop
    move    $s1, $v0

    move    $a0, $t2
    move    $a1, $t0
    jal fetchaddress
    nop
    move    $s2, $v0

gauss_reduct_cicloi:
    bgt $t0, $t5,gauss_reduct_fim_ciclo_i
    nop

    ldc1    $f6,($s1)
    ldc1    $f8,($s2)

    mul.d   $f8,$f8,$f4
    sub.d   $f6,$f6,$f8
    sdc1    $f6,($s1)
    addiu   $t0,$t0,1

    addiu   $s1,$s1,8
    addiu   $s2,$s2,8

    j   gauss_reduct_cicloi
    nop

gauss_reduct_fim_ciclo_i:

gauss_reduct_cicloj_continue:
    addiu   $t1,$t1,1
    j   gauss_reduct_cicloj
    nop

gauss_reduct_fim_ciclo_j:
    addiu   $t2,$t2,1
    j   gauss_reduct_ciclok
    nop

gauss_reduct_end:
    lw  $ra,  20($sp)
    lw  $s2,  16($sp)
    lw  $s1,  12($sp)
    lw  $s0,  8($sp)
    lw  $a1,  4($sp)
    lw  $a0,  0($sp)
    addiu   $sp,  $sp, 24

    jr  $ra
    nop

gauss_solve:
    addiu   $sp,  $sp, -24
    sw  $ra,  20($sp)
    sw  $s2,  16($sp)
    sw  $s1,  12($sp)
    sw  $s0,  8($sp) 
    sw  $a1,  4($sp) 
    sw  $a0,  0($sp)

    add $t3, $a0, $zero
    addi    $t0, $a1, -1
    addi    $t5, $a1, 0

    sll $s1, $t4, 3
    addu $s1, $s1, $a2

    addi    $t0, $t4, 0
gauss_solve_cicloi:
    blt $t0, $zero, gauss_solve_end
    nop

    # v0 = &A[i][n]
    move    $a0, $t0
    move    $a1, $t5
    jal fetchaddress
    nop

    # $f6 = A[i][n]
    ldc1    $f6,($v0)

    # X[i] = A[i][n]
    sdc1    $f6,($s1)

    addi    $t1, $t0, 1

    sll $s2, $t1, 3
    add $s2, $s2, $a2

gauss_solve_cicloj:
    beq $t1, $t5, gauss_solve_fim_cicloi
    nop

    # v0 = &A[i][j]
    move    $a0, $t0
    move    $a1, $t1
    jal fetchaddress
    nop

    ldc1    $f8,($v0)
    ldc1    $f4,($s2)
    mul.d   $f8,$f8,$f4

    sub.d   $f6,$f6,$f8
    sdc1    $f6,($s1)

    addi    $t1,$t1,1
    addi    $s2, $s2, 8
    j   gauss_solve_cicloj
    nop

gauss_solve_fim_cicloi:

    # v0 = &A[i][i]
    move    $a0, $t0
    move    $a1, $t0
    jal fetchaddress
    nop

    # $f8 = A[i][i]
    ldc1    $f8,($v0)

    # x[i] = x[i] / A[i][i];
    div.d   $f6,$f6,$f8
    sdc1    $f6,($s1)

    subi    $t0,$t0,1
    subi    $s1, $s1, 8
    j   gauss_solve_cicloi
    nop

gauss_solve_end:
    lw  $ra,  20($sp)
    lw  $s2,  16($sp)
    lw  $s1,  12($sp)
    lw  $s0,  8($sp)
    lw  $a1,  4($sp)
    lw  $a0,  0($sp)
    addiu   $sp,  $sp, 24

    jr  $ra
    nop

fetchaddress:
    addiu   $t5,$t5,1
    multu   $a0, $t5
    subiu   $t5,$t5,1
    mflo    $v0
    add $v0, $v0, $a1
    sll $v0, $v0, 3
    add $v0, $v0, $t3
    jr  $ra
    nop


print_matrix:
    addiu   $sp,  $sp, -24
    sw  $ra,  20($sp)
    sw  $s2,  16($sp)
    sw  $s1,  12($sp)
    sw  $s0,  8($sp) 
    sw  $a2,  4($sp) 
    sw  $a0,  0($sp)

    move    $s2,  $a0
    move    $s1,  $zero
    
loop_s1:
    addi    $a2,$a1,1
    move    $s0,  $zero
loop_s0:
    l.d $f12, 0($s2)
    li  $v0,  3
    syscall
    la  $a0,  spaces
    li  $v0,  4
    syscall

    addiu   $s2,  $s2, 8

    addiu   $s0,  $s0, 1
    blt $s0,  $a2, loop_s0
    nop
    la  $a0,  newline
    syscall
    addiu   $s1,  $s1, 1
    blt $s1,  $a1, loop_s1
    nop
    la  $a0,  newline
    syscall

    lw  $ra,  20($sp)
    lw  $s2,  16($sp)
    lw  $s1,  12($sp)
    lw  $s0,  8($sp)
    lw  $a2,  4($sp)
    lw  $a0,  0($sp)
    addiu   $sp,  $sp, 20

    jr  $ra             # return
    nop


print_solution:
    li $v0, 4   #display a mensagem
    la $a0, Solution
    syscall
    li $v0, 4   #display a mensagem
    la $a0, newline
    syscall

    addiu   $sp,  $sp, -24
    sw  $ra,  20($sp)
    sw  $s2,  16($sp)
    sw  $s1,  12($sp)
    sw  $s0,  8($sp) 
    sw  $a2,  4($sp) 
    sw  $a0,  0($sp)

    move    $s1,  $zero
    move    $s2,  $a2

print_solution_loop_s0:

    ldc1    $f12, ($s2)
    li  $v0,  3
    syscall

    addiu   $s2,  $s2, 8
    addiu   $s1,  $s1, 1
    la  $a0,  newline
    li  $v0,  4
    syscall
    blt $s1,  $a1, print_solution_loop_s0
    nop

    lw  $ra,  20($sp)
    lw  $s2,  16($sp)
    lw  $s1,  12($sp)
    lw  $s0,  8($sp)
    lw  $a2,  4($sp)
    lw  $a0,  0($sp)
    addiu   $sp,  $sp, 20

    jr  $ra
    nop

### End of text segment

### Data segment 
    .data
array: .double
 2.0, 1.0, -3.0, -1.0,
 -1.0, 3.0, 2.0, 12.0,
 3.0, 1.0, -3.0, 0.0
 
### String constants
spaces:
    .asciiz "   "
newline:
    .asciiz "\n"

Solution:.asciiz "Solution:"



solution:
    .double 0.0
    .double 0.0
    .double 0.0
    .double 0.0



### End of data segment

You've already noted change data from using .double to .float .

In general, especially if you want your data structures to shrink for using float instead of double, scaling for array indexing and pointer arithmetic will be changed as well.

As for instructions: just change the .d 's to .s 's. (It it were me, I'd leave the registers as is with even numbers.)

Syscalls also change from the double input & print to the single versions.

Stack locations may shrink, but like the coprocessor registers, I'd probably leave them alone and waste the space, so it more easily can be converted back to double if desired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM