Convert integer to ASCII and save to a file in the MIPS Assembly

0

I need to have my code save the numbers generated in a file. I suppose, I have to convert these numbers to ASCII before trying to save them to the file, but I'm finding it very difficult to be able to get these numbers to be converted and save to the files because I don't have much experience with assembly. Could someone help me with this? According to MARS, when you go through the line "sb $ t2, 0 ($ t1)" the following happens: Runtime exception at 0x00400230: address out of range 0x00000000 and the ordered values no longer appear

.data
    vet:    .space 4000
    maior:  .word   0
    Menor:  .word   0
    space:  .asciiz " "     # uma cadeia de espaço.
    line:   .asciiz "\n"        # uma sequência de nova linha.
    menor_numero:   .asciiz "Menor numero Lido: "
    maior_numero:   .asciiz "Maior numero Lido: "
    size:   .word   15
    sorted_array_string:    .asciiz "Ordenado:"
    fout:   .asciiz "text.txt"      # filename for output
    buffer: .resb 33                   # reserve um espaço de 32 caracteres
.text
    la $s0, vet         #s0 e o endereço base do vetor
    add $t0, $0, 15     #t0 = N

    add $t1, $0, $0     #t1 = i = 0
    addi $s7, $0, -2000000  #varialvel para armazenamento do maior numero
    addi $s6, $0, 2000000   #verialvel para armazenamento do menor numero

ini_loop:
    beq $t1, $t0, fim_loop
        #ler numero
        addi $v0, $0, 5
        syscall
        add $t4, $0, $v0
        #salver o numero na memoria
        sw $t4, 0($s0)
        addi $s0, $s0, 4
        #verificar se o numero e o maior
        bgt $s7, $t4, depoismaior
        add $s7, $0, $t4    #numero maior = t4
    depoismaior:
        #verificar se o numero e o menor
        blt $s6, $t4, naomenor
        add $s6, $0, $t4    #numero menor = t4
    naomenor:
    addi $t1, $t1, 1    # i++       
    j ini_loop
fim_loop:

    #print do maior numero
    li  $v0, 4  
    la  $a0, maior_numero
    syscall
    addi $v0, $0, 1
    add $a0, $0, $s7
    syscall         #print
    li  $v0, 4
    la  $a0, line
    syscall
    #print do menor numero
    li  $v0, 4  
    la  $a0, menor_numero
    syscall
    addi $v0, $0, 1
    add $a0, $0, $s6
    syscall         #print
    li  $v0, 4
    la  $a0, line
    syscall

sort_prep:
    la  $t0, vet        # Carregar matriz para $t0.
    lw  $t1, size       # carregar o tamanho da matriz para $t1.
    li  $t2, 1          # corredor de loop, começando em 1.
sort_xloop:
    la  $t0, vet        # Carregar matriz para $t0.
    bge $t2, $t1, sort_xloop_end    # while (t2 < $t1).
    move    $t3, $t2        # copy $t2 to $t3.
sort_iloop:
    la  $t0, vet        # Carregar matriz para $t0.
    mul $t5, $t3, 4     # multiplique $ t3 por 4 e armazene em $ t5
    add $t0, $t0, $t5       # adicione o endereço da matriz com $ t5, que é o índice multiplicado por 4.
    ble $t3, $zero, sort_iloop_end  # while (t3 > 0).
    lw  $t7, 0($t0)     # matriz de carregamento [$ t3] para $ t7.
    lw  $t6, -4($t0)        # load array[$t3 - 1] to $t6.
    bge $t7, $t6, sort_iloop_end    # while (array[$t3] < array[$t3 - 1]).
    lw  $t4, 0($t0)
    sw  $t6, 0($t0)
    sw  $t4, -4($t0)
    subi    $t3, $t3, 1
    j   sort_iloop      # pule de volta para o início do sort_iloop.
sort_iloop_end:
    addi    $t2, $t2, 1     # incrementar corredor de loop por 1.
    j   sort_xloop      # pule de volta para o início do sort_xloop.
sort_xloop_end:
    li  $v0, 4          # 4 = syscall de string de impressão.
    la  $a0, sorted_array_string    # Carregar ordenado_array_string no registro de argumentos $a0.
    syscall             # emitir uma chamada do sistema.
    li  $v0, 4          # 4 = syscall de string de impressão.
    la  $a0, line       # linha de carregamento no registro de argumentos $a0.
    syscall             # emitir uma chamada do sistema.
    jal print           # rotina de impressão de chamadas.
exit:
    li  $v0, 10         # 10 = exit syscall.
    syscall             # emitir uma chamada do sistema.

print:
print_loop_prep:
    la  $t0, vet
    lw  $t1, size
    li  $t2, 0
print_loop:
    bge $t2, $t1, print_end
    li  $v0, 1
    lw  $a0, 0($t0)
    syscall
    li  $v0, 4
    la  $a0, space
    syscall
    addi    $t0, $t0, 4
    addi    $t2, $t2, 1
    j   print_loop
print_end:
    li  $v0, 4
    la  $a0, line
    syscall



A:
  bnez $a0, ItoA.non_zero  # first, handle the special case of a value of zero
  nop
  li   $t0, '0'
  sb   $t0, 0($t0)
  sb   $zero, 1($t0)
  li   $v0, 1
  jr   $ra
ItoA.non_zero:
  addi $t0, $zero, 10     # now check for a negative value
  li $v0, 0

  bgtz $a0, ItoA.recurse
  nop
  li   $t1, '-'
  sb   $t1, 0($t0)
  addi $v0, $v0, 1
  neg  $a0, $a0
ItoA.recurse:
  addi $sp, $sp, -24
  sw   $fp, 8($sp)
  addi $fp, $sp, 8
  sw   $a0, 4($fp)
  sw   $a1, 8($fp)
  sw   $ra, -4($fp)
  sw   $s0, -8($fp)
  sw   $s1, -12($fp)

  div  $a0, $t0       # $a0/10
  mflo $s0            # $s0 = quotient
  mfhi $s1            # s1 = remainder  
  beqz $s0, ItoA.write
ItoA.continue:
  move $a0, $s0  
  jal ItoA.recurse
  nop
ItoA.write:
  add  $t1, $a1, $v0
  addi $v0, $v0, 1    
  addi $t2, $s1, 0x30 # convert to ASCII
  sb   $t2, 0($t1)    # store in the buffer
  sb   $0, 1($t1)

ItoA.exit:
  lw   $a1, 8($fp)
  lw   $a0, 4($fp)
  lw   $ra, -4($fp)
  lw   $s0, -8($fp)
  lw   $s1, -12($fp)
  lw   $fp, 8($sp)    
  addi $sp, $sp, 24






li $t0, 1
li $t1, 2
add $t3, $t0, $t1
la $t0, buffer
sb $t3, ($t0)

# Open (for writing) a file that does not exist
li   $v0, 13       # system call for open file
la   $a0, fout     # output file name
li   $a1, 1        # Open for writing (flags are 0: read, 1: write)
li   $a2, 0        # mode is ignored
syscall            # open a file (file descriptor returned in $v0)
move $s6, $v0      # save the file descriptor 

# Write to file just opened
li   $v0, 15       # system call for write to file
move $a0, $s6      # file descriptor 
move $a1, $t0      # address of buffer from which to write
li   $a2, 33       # hardcoded buffer length
syscall            # write to file

# Close the file 
li   $v0, 16       # system call for close file
move $a0, $s6      # file descriptor to close
syscall            # close file
file
assembly
integer
ascii
mips
asked on Stack Overflow Apr 12, 2020 by Sullyvan Marks • edited Apr 12, 2020 by Sullyvan Marks

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0