从文件中读取十六进制数并更改其表示形式

时间:2011-01-15 16:42:18

标签: assembly hex mips

我想编写一个程序,将汇编源文件中找到的所有十六进制数的表示法从传统(h)更改为C样式(0x)。 我已经启动了编码部分,但我不确定如何检测十六进制数字并最终更改样式并将其保存回文件...

我已经开始编写程序..

## Mips program  - 

.data
fin:   .ascii ""      # filename for input
msg0:   .asciiz "aaaa"
msg1:   .asciiz "Please enter the input file name:"
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

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 checkhexa       #check for hexadecimal numbers

    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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我要做的是(伪代码):

while EOF not reached
    read from file until space or newline reached
    check if last letter is an 'h'

    if it is
        check if it's a valid number

        if it is
            write to output file '0x'
            write to output file the number without last letter

            continue loop
        end if
    else
        write word to output file
    end if
end while

我不知道MIPS汇编,但我非常擅长x86,如果这可以帮助......