Learning MIPS assembly: read text and write to file -
i'm trying write simple programme in mips assembly language. i'm trying read multiple characters keyboard , save file. i'm creating file 13 opcode , saving characters 15 opcode. don't understand: how dynamically assign number of characters write in $a2 15 opcode (line 37, hardcoded). can't figure out how print numbers of characters written file ($v0 contains value after writting file, line 49).
now programme throwing error: line 49: runtime exception @ 0x00400078: address out of range 0x0000002c
here code:
.data handle_text: .space 100 # buffor of 100 characters, bits, bytes? out_file: .asciiz "file_out.txt" # out file asklabel: .asciiz "\please come in string save\n" countlabel: .asciiz "\characters typed:\n" .text main: la $a0, asklabel # text print li $v0, 4 # opcode syscall la $a0, handle_text # set text la $a1, handle_text # number of characters write li $v0, 8 # opcode syscall li $v0, 13 # scheme phone call open file la $a0, out_file # output file name li $a1, 1 # open writing (flags 0: read, 1: write) li $a2, 0 # mode ignored syscall # open file (file descriptor returned in $v0) move $s6, $v0 # save file descriptor move $a0, $s6 # file handle la $a1, handle_text # text print #line 37 li $a2, 44 # text length li $v0, 15 # opcode syscall move $t1, $v0 # move v0 t1 v0 won't 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 null terminated? li $v0, 16 # scheme phone call close file move $a0, $s6 # file descriptor close syscall # close file li $v0, 10 # close app syscall
first of all
# buffor of 100 characters, bits, bytes? they bytes, not bits. also, string
la $a1, handle_text at line 21 wrong, cause have load number, not address. can utilize example
.data legnth: .word 100 [...] lw $a1, length also, in mind can read 99 characters, cause lastly has '/0'. @ line 37, still can utilize
lw $a2, lenght instead of hardcoded 44.
about this
# error. maybe it's because string should null terminated? no, it's because want print string, while $t1=$v0 integer. print number of read characters, have phone call syscall 1 (not 4).
to reply question, it's not simple pass ordinary number argument, because have set maximum number 1 way or another. example, in way, output 100, if type in 10 characters. simplest way solve "loop", like
li $t0, -1 loop: addi $t0, $t0, 1 lw $t1, handle_text($t0) bnez $t1, loop addi $t0, $t0, -1 at end of code, $t0 contains exact number of characters (excluding '\0' , '\n').
assembly mips
No comments:
Post a Comment