每个字符的频率

时间:2018-10-13 18:01:30

标签: arrays file assembly mips mars-simulator

我正在尝试计算文件中的字符数,我已经读取了文件并将其存储在数组中,我开始只用一个字母(a)进行处理,剩下的我就去做,我面临的问题是,当我加载数组的地址并检查数组中第一个元素的值是否为字母(a)时,如果不是,则分支为字母(b),否则为该值是相同的,我将计数器的值递增,然后转到下一个地址。

问题是我尝试了一种方法,但是它始终使($ t0)的值保持不变(97),所以我尝试了另一种解决方案,但是它似乎不起作用

任何想法或帮助将不胜感激

.data

.align 2
A: .space 10000 # space for 10000 Char

file: .asciiz "input.txt"  # the txt file is in the same dirictory as MARS 
tool and the source code

error: .asciiz "File does not exist"


.text
.globl main

main:

la $t0, A
# Open file for reading

li   $v0, 13       # system call for open file
la   $a0, file      # input file name
li   $a1, 0           # flag for reading
li   $a2, 0        # mode is ignored
syscall               # open a file 
move $s6, $v0         # save the file descriptor 

bltz   $s6,noFile
# checking the value of $s6 if it saved values means the file exist if not 
there's not file
# if file is not found exit - Branch if $a0 is 0 which mean no data


# reading from file to Array (A)

li   $v0, 14        #  reading from file
move $a0, $s6       # file descriptor 
la   $a1, A    # address of buffer from which to read
li   $a2, 10000 
syscall             # read from file




  CountA:

 lb $t0 , 0($a1)

 bne $t0,'a', CountB
 addi $t1,$t1,1 #counter
 addi $a1,$a1,4 # to move to next address

 addi $t0,$t0,1 # to incr t0 to check for next char

 b CountA

 CountB:

 # not completed yet



 exit:
 # Close the file 
 li   $v0, 16       # system call for close file
 move $a0, $s0      # file descriptor to close
 syscall            # close file

 li $v0, 10    # Exit
 syscall

 noFile:            # if file is not found show message and exit

 li $v0, 4
 la $a0,error #load error msg
 syscall # print error msg

 li $v0, 10    # Exit
 syscall

input.txt的示例

a
a
a
b
b
c
c
d

0 个答案:

没有答案