मैं एक एमआईपीएस असेंबली कोड लिख रहा हूं जो उपयोगकर्ता को फ़ाइल नाम के लिए पूछेगा और यह फ़ाइल की सामग्री के बारे में कुछ आंकड़े तैयार करेगा।एमआईपीएस असेंबली में उपयोगकर्ता इनपुट से फ़ाइल नाम पढ़ना
हालांकि, जब मैं शुरुआत से एक चर में फ़ाइल नाम हार्ड कोड करता हूं तो यह ठीक काम करता है, लेकिन जब मैं उपयोगकर्ता को फ़ाइल नाम इनपुट करने के लिए कहता हूं तो यह काम नहीं करता है।
कुछ डीबगिंग के बाद, मैंने पाया है कि प्रोग्राम मेमोरी में उपयोगकर्ता इनपुट के अंत में 0x00 char और 0x0a char (asciitable.com देखें) जोड़ता है और इसीलिए यह उपयोगकर्ता इनपुट के आधार पर फ़ाइल नहीं खोलता है।
किसी को भी उन अतिरिक्त वर्णों से छुटकारा पाने के बारे में कोई जानकारी है, या उपयोगकर्ता से अपना नाम प्राप्त करने के बाद फ़ाइल को कैसे खोलें ??
यहाँ है मेरा पूरा कोड (यह उपयोगकर्ता बात से फ़ाइल नाम के अलावा ठीक काम कर रहा है, और किसी को भी किसी भी उद्देश्य के लिए उन्होंने इसका इस्तेमाल करने के लिए स्वतंत्र है/वह चाहता है):
.data
fin: .ascii "" # filename for input
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 #read from file
move $s1, $v0 #$t0 = total number of bytes
li $t0, 0 # Loop counter
li $t1, 0 # Uppercase counter
li $t2, 0 # Lowercase counter
li $t3, 0 # Decimal counter
li $t4, 0 # Words counter
loop:
bge $t0, $s1, end #if end of file reached OR if there is an error in the file
lb $t5, buffer($t0) #load next byte from file
jal checkUpper #check for upper case
jal checkLower #check for lower case
jal checkDecimal #check for decimal
jal checkWord #check for words
addi $t0, $t0, 1 #increment loop counter
j loop
end:
jal output
jal fileClose
li $v0, 10
syscall
fileRead:
# Open file for reading
li $v0, 13 # system call for open file
la $a0, fin # input file name
li $a1, 0 # flag for reading
li $a2, 0 # mode is ignored
syscall # open a file
move $s0, $v0 # save the file descriptor
# reading from file just opened
li $v0, 14 # system call for reading from file
move $a0, $s0 # file descriptor
la $a1, buffer # address of buffer from which to read
li $a2, 100000 # hardcoded buffer length
syscall # read from file
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 #branch if less than 'A'
bgt $t5, 0x5a, L1 #branch if greater than 'Z'
addi $t1, $t1, 1 #increment Uppercase counter
L1:
jr $ra
checkLower:
blt $t5, 0x61, L2 #branch if less than 'a'
bgt $t5, 0x7a, L2 #branch if greater than 'z'
addi $t2, $t2, 1 #increment Lowercase counter
L2:
jr $ra
checkDecimal:
blt $t5, 0x30, L3 #branch if less than '0'
bgt $t5, 0x39, L3 #branch if greater than '9'
addi $t3, $t3, 1 #increment Decimal counter
L3:
jr $ra
checkWord:
bne $t5, 0x20, L4 #branch if 'space'
addi $t4, $t4, 1 #increment words counter
L4:
jr $ra
fileClose:
# Close the file
li $v0, 16 # system call for close file
move $a0, $s0 # file descriptor to close
syscall # close file
jr $ra
नोट: मैं 'मंगल ग्रह सिम्युलेटर का उपयोग हूँ, कि अगर बनाता है किसी भी अलग
अद्यतन: मैं लिख और निम्न प्रक्रिया को फोन करके समस्या को हल कर दिया है:
nameClean:
li $t0, 0 #loop counter
li $t1, 21 #loop end
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
धन्यवाद मैं इस समस्या का समाधान कर लिया लेखन और निम्न कार्यविधि
फोन करके, –