2016-04-22 74 views
0

我想打印一些文件的内容(字符串和浮点数)。Mips-打印到文件

这是我到目前为止已经实现:

.data: 
    line_break: .asciiz "\n" 
    buffer: .space 1024 
.text: 
main: 
    addi $t0, $zero, -1 
    jal open_file       # open the file to write to 
    beq $v0, $t0, create_file    # if return value -1 --> file not available --> create the file 
    move $s6, $v0       # save the file descriptor 
    [...] 
    ulw $t0, print_initiaton_message  # save the print_initiaton_message in a temp 
    sw $t0, buffer       # put print_initiaton_message on buffer 
    li $v0, 15        # syscall to write to file 
    move $a0, $s6       # move file descriptor to $a0 
    la $a1, buffer       # target to write from 
    li $a2, 1024       # amount to be written 
    syscall         # syscall to write in file 
    [...] 
    s.s $f12, buffer 
    li $v0, 15        # syscall to write to file 
    move $a0, $s3       # move file descriptor to $a0 
    la $a1, buffer       # target to write from 
    li $a2, 4        # amount to be written 
    syscall         # syscall to write in file 
    [...] 

的基本思想是把必要的信息在缓冲区中,然后执行系统调用。

它似乎工作 - 因为正确创建文件,然后打开和关闭。还有一个在它的内容,但没有预期的结果:

’®)@ 

PÀ<@ 

[...] 

在第一的位置,应该有一个字符串,然后换行,接着是浮点。

现在我的问题: - 我如何才能实现我的输出格式? - 如果我的输入超过缓冲区大小,那么缓冲区大小是什么意思,会发生什么? - 写入的金额是多少?

我试图通过几个系统调用引用(即this one),查找示例(和found thisthat),但主要问题是它们只提供代码,并未涵盖上述问题。

回答

0

我终于找到了解决办法:

我设定数字/字符串1024印的大小。因此,我的程序从缓冲区地址中获取内容,并从堆(或数据)部分额外打印1023个字符。我通过计算字符串中字符的数量(因为它是一个用于教育目的的项目,这是可以的)并将大小设置为字符数量来解决此问题;而\ n是一个字节。

我的程序打印的奇怪字符是代表其相应十六进制值的ASCII符号。我的错误是假设我必须从左至右阅读字符。由于MIPS正在使用Big Endian format,所以必须从右向左读取ASCII字符。创建一个十六进制转储并计算相应的浮点数导致正确的结果。

为了避免混淆的基于ASCII的输出需要额外的字符串转换想法是为每个数字计算其对应的ASCII字符,然后打印结果值。

0

sw $t0, buffer

这行代码将缓冲区的前32位设置为print_initiaton_message地址。我不熟悉文件I/O系统调用;但是,我不知道如果你真的想这样做:

li $v0, 15        # syscall to write to file 
move $a0, $s6       # move file descriptor to $a0 
la $a1, print_initiation_message  # target to write from 
li $a2, <actual length of initiation message> 
syscall         # syscall to write in file 
+0

根据MIPS指令参考([可在此获得](http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html))sw将$ t0的内容保存到指定的地址,缓冲区。我想将previos子过程的输出保存到缓冲区,以便我可以打印它。 –

+0

什么数据类型是缓冲区的输出?如果输出是字符串,则需要将该字符串复制到缓冲区中。如果输出是单个整数,则需要首先将整数格式化为字符串。 – Zack

+0

是的,请参阅下面的答案。我找到了解决方案 –