assembly Mips从File和buffer中读取文本

时间:2016-05-26 19:18:35

标签: assembly mips

如何从file.txt中读取文本并像字符串一样保存在数据段中?例如,如果文件中的文本是" Hello World!",如何阅读并修改它?

在系统调用14中,缓冲区是什么用的?我虽然是将指针保存到文件文本。

1 个答案:

答案 0 :(得分:0)

此程序读取文件并反转文件中的文本。我希望它对你有用。

.data  
fin: .asciiz "file.txt"      # filename for input
buffer: .space 128
buffer1: .asciiz "\n"
val : .space 128
newline: .asciiz "\n"
ans: .asciiz " The String reversed is "
.text

################################################ 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,  11  # hardcoded buffer length
syscall            # read from file

#li  $v0, 4          # 
la  $a0, buffer     # buffer contains the values
syscall             # print int

lb $t1 , buffer


la      $a0, buffer     #calling opening prompt
#li      $v0, 4
syscall
la      $a0, buffer        #initial string
syscall
la      $a0, newline    #newline
syscall
la      $a0, ans        #initial text for reversed string
syscall
li      $t2, 0
strLen:                 #getting length of string
lb      $t0, buffer($t2)   #loading value
add     $t2, $t2, 1
bne     $t0, $zero, strLen

li      $v0, 11         #load immediate - print low-level byte
Loop:
sub     $t2, $t2, 1     #this statement is now before the 'load address'
la      $t0, buffer($t2)   #loading value
lb      $a0, ($t0)
syscall
#This is where the sub statement used to be, which caused the loop to terminate too early
bnez    $t2, Loop
li      $v0, 10              #program done: terminating
syscall



# Close the file 

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

<强> file.txt的

Hello World

<强>输出

dlroW olleH

enter image description here

机器代码是:

00100100000000100000000000001101
00111100000000010001000000000001
00110100001001000000000000000000
00100100000001010000000000000000
00100100000001100000000000000000
00000000000000000000000000001100
00000000000000101000000000100001
00100100000000100000000000001110
00000000000100000010000000100001
00111100000000010001000000000001
00110100001001010000000000001001
00100100000001100000000000001011
00000000000000000000000000001100
00111100000000010001000000000001
00110100001001000000000000001001
00000000000000000000000000001100
00111100000000010001000000000001
10000000001010010000000000001001
00111100000000010001000000000001
00110100001001000000000000001001
00000000000000000000000000001100
00111100000000010001000000000001
00110100001001000000000000001001
00000000000000000000000000001100
00111100000000010001000000000001
00110100001001000000000100001011
00000000000000000000000000001100
00111100000000010001000000000001
00110100001001000000000100001101
00000000000000000000000000001100
00100100000010100000000000000000
00111100000000010001000000000001
00000000001010100000100000100001
10000000001010000000000000001001
00100001010010100000000000000001
00010101000000001111111111111011
00100100000000100000000000001011
00100000000000010000000000000001
00000001010000010101000000100010
00111100000000010001000000000001
00110100001000010000000000001001
00000001010000010100000000100000
10000001000001000000000000000000
00000000000000000000000000001100
00010101010000001111111111111000
00100100000000100000000000001010
00000000000000000000000000001100
00100100000000100000000000010000
00000000000101100010000000100001
00000000000000000000000000001100
相关问题