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

发布于 2024-10-12 07:54:33 字数 1553 浏览 1 评论 0原文

我想编写一个程序,将汇编源文件中找到的所有十六进制数字的表示法从传统 (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

任何帮助将不胜感激。

I want to write a program changing the notation of all hexadecimal numbers found in an assembly source file from traditional (h) to C-style (0x).
I have started the coding part but am not sure how can I detect the hexadecimal numbers and eventually change the style and save it back in the file...

I have started writing the program..

## 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

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿与i 2024-10-19 07:54:33

我会做的是(伪代码):

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,如果这可以帮助......

What I would do is (in pseudocode):

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

I don't know MIPS assembly, but I'm quite good at x86, if this can help...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文