2016-04-22 72 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

是的,請參閱下面的答案。我找到了解決方案 –