1

MIPS アセンブリ言語で簡単なプログラムを作成しようとしています。私がやろうとしているのは、キーボードから複数の文字を読み取り、それをファイルに保存することです。13 個のオペコードでファイルを作成し、15 個のオペコードで文字を保存しています。理解できません: 15 オペコードの $a2 に書き込む文字数を動的に割り当てる方法 (37 行目、現在はハードコードされています)。また、ファイルに書き込まれた文字数を出力する方法がわかりません ($v0 には、ファイルへの書き込み後、49 行目にこの値が含まれます)。

現在、プログラムはエラーをスローしています: 49 行目: 0x00400078 でのランタイム例外: 範囲外のアドレス 0x0000002c

これが私のコードです:

.data

handle_text:
    .space 100 # buffor of 100 characters, bits, bytes?

out_file:
    .asciiz "file_out.txt" # out file

asklabel:
    .asciiz "\Please enter string to save\n" 

countlabel:
    .asciiz "\Characters typed:\n"

.text
main:
    la $a0, asklabel # text to print
    li $v0, 4 # opcode
    syscall

    la $a0, handle_text # Where to put text
    la $a1, handle_text # Number of characters to write
    li $v0, 8 # opcode
    syscall 

    li $v0, 13       # system call for open file
    la $a0, out_file     # 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 

    move $a0, $s6 # file handle
    la $a1, handle_text # text to print
#line 37
    li $a2, 44 # TEXT LENGTH
    li $v0, 15 # opcode
    syscall

    move $t1, $v0 # move v0 to t1 so v0 won't be overwritten

    la $a0, countlabel # show text 
    li $v0, 4 # op code
    syscall 

    move $a0, $t1 # place characters amount in $a0
    li $v0, 4 # opcode
    syscall
# ERROR. Maybe it's becouse string should be null terminated?

    li   $v0, 16       # system call for close file
    move $a0, $s6      # file descriptor to close
    syscall            # close file

    li $v0, 10  # close app
    syscall 
4

1 に答える 1