I am writing a MIPS assembly code that will ask the user for a file name and it will create some statistics about the contents of the file.
However, when I hardcode the file name into a variable from the very beginning, it works fine, but when I ask the user to enter the file name, it doesn't work.
after some debugging, I found that the program adds 0x00 char and 0x0a char (check asciitable.com) at the end of user input into memory and therefore does not open the file based on user input.
Does anyone know how to get rid of these extra characters, or how to open a file after getting its user name?
here is my complete code (it works fine, except for the file name from the user thing, and anyone can use it for any purpose that he / she wants):
.data
fin: .ascii ""
msg0: .asciiz "aaaa"
msg1: .asciiz "Please enter the input file name:"
msg2: .asciiz "Number of Uppercase Char: "
msg3: .asciiz "Number of Lowercase Char: "
msg4: .asciiz "Number of Decimal Char: "
msg5: .asciiz "Number of Words: "
nline: .asciiz "\n"
buffer: .asciiz ""
.text
li $v0, 4
la $a0, msg1
syscall
li $v0, 8
la $a0, fin
li $a1, 21
syscall
jal fileRead
move $s1, $v0
li $t0, 0
li $t1, 0
li $t2, 0
li $t3, 0
li $t4, 0
loop:
bge $t0, $s1, end
lb $t5, buffer($t0)
jal checkUpper
jal checkLower
jal checkDecimal
jal checkWord
addi $t0, $t0, 1
j loop
end:
jal output
jal fileClose
li $v0, 10
syscall
fileRead:
li $v0, 13
la $a0, fin
li $a1, 0
li $a2, 0
syscall
move $s0, $v0
li $v0, 14
move $a0, $s0
la $a1, buffer
li $a2, 100000
syscall
jr $ra
output:
li $v0, 4
la $a0, msg2
syscall
li $v0, 1
move $a0, $t1
syscall
li $v0, 4
la $a0, nline
syscall
li $v0, 4
la $a0, msg3
syscall
li $v0, 1
move $a0, $t2
syscall
li $v0, 4
la $a0, nline
syscall
li $v0, 4
la $a0, msg4
syscall
li $v0, 1
move $a0, $t3
syscall
li $v0, 4
la $a0, nline
syscall
li $v0, 4
la $a0, msg5
syscall
addi $t4, $t4, 1
li $v0, 1
move $a0, $t4
syscall
jr $ra
checkUpper:
blt $t5, 0x41, L1
bgt $t5, 0x5a, L1
addi $t1, $t1, 1
L1:
jr $ra
checkLower:
blt $t5, 0x61, L2
bgt $t5, 0x7a, L2
addi $t2, $t2, 1
L2:
jr $ra
checkDecimal:
blt $t5, 0x30, L3
bgt $t5, 0x39, L3
addi $t3, $t3, 1
L3:
jr $ra
checkWord:
bne $t5, 0x20, L4
addi $t4, $t4, 1
L4:
jr $ra
fileClose:
li $v0, 16
move $a0, $s0
syscall
jr $ra
Note. I use MARS Simulator if it does any other
Update: I solved the problem by writing and naming the following procedure:
nameClean:
li $t0, 0
li $t1, 21
clean:
beq $t0, $t1, L5
lb $t3, fin($t0)
bne $t3, 0x0a, L6
sb $zero, fin($t0)
L6:
addi $t0, $t0, 1
j clean
L5:
jr $ra