【问题标题】:Using QtSpim to read a .txt file and edit it to have proper capitalization in assembly code使用 QtSpim 读取 .txt 文件并对其进行编辑以在汇编代码中正确大写
【发布时间】:2016-10-20 03:51:53
【问题描述】:
.data
fnf:    .ascii  "The file was not found: "
###important: UPDATE THIS PATH TO WHERE YOU SAVED THE TEXT FILE
#asciiz directive creates null-terminated string
file:   .asciiz "C:/Users/mmono/OneDrive/Documents/input.txt" 
pstring:.asciiz " characters.\nFile contents:"
buffer: .space 1024     
    .text

# These lines opens and reads the file containing text to be modified
main:
    li  $v0, 13     # System call to open file, $v0 set to file descriptor
                # $v0 negative if failed to open file
    la  $a0, file   # Load file to read, $a0 set to address of string
                # containing file name
    li  $a1, 0      # Set read-only flag
    li  $a2, 0      # Set mode
    syscall
    add $s0, $v0, $zero # Save file descriptor in $v0 to new register $s0
                # because $v0 will be used in other system calls
    blt $v0, 0, err # Go to handler if failed to open file

# These lines read text from file
read:
    li  $v0, 14     # System call to read file
    add $a0, $s0, $zero # Load file descriptor to $a0
    la  $a1, buffer # Set $a1 to address of input buffer where
                # text will be loaded to
    li  $a2, 1024   # Set $a2 to number of characters to read
    syscall
    #after read, $v0 will have number of bytes read
    #set last byte to null
    la  $a0, buffer
    add $a0, $a0, $v0   #address of byte after file data
    sb  $zero, 0($a0)

#initialize some registers
init:
    li  $t0, 0      # $t0 will be message character iterator i
                # initialize i=0
    add $s1, $a1, $zero # save address start of buffer

#loop to count characters, exclude white spaces and 
loop:
    add $s2, $s1, $t0   # $s2 <= A + i, address of current character
                # A: address start of buffer
    lb  $s3, 0($s2) # load char in message[i] to $s3
    beq $s3, $zero, print   #null, reached end of buffer
    addi    $t0, $t0, 1 #i++
    j   loop        #continue counting

# Print Data to console
print:
    li  $v0, 1      # System call to print integer
    add $a0, $t0, $zero # Load to $a0 integer to print
    syscall
    #print "File contents:"
    li  $v0, 4      # System call to print string
    la  $a0, pstring    # Load to $a0 string to print
    syscall
    #print actual file contents
    li  $v0, 4      # System call to print string
    la  $a0, buffer # Load to $a0 string to print
    syscall

# Close File
close:
    li  $v0, 16     # Close File Syscall
    add $a0, $s0, $zero # Load File Descriptor
    syscall
    j   done        # Goto done

# Error
err:
    li  $v0, 4      # System call to print string
    la  $a0, fnf    # Load Error String
    syscall

# Done
done:
    li  $v0, 10     # Exit Syscall
    syscall

这是程序读取的文件。它被称为 input.txt。

这是非常有用的,但我希望有更多的图片 哇!!!!!!!!!!!太酷了! 我认为你应该写一篇关于这个问题的后续文章。我们需要像您这样更有见地的意见。 伟大的!再写一个!


程序需要编辑 .txt 文件,使句号后的第一个字母、换行符(换行符)、感叹号或问号大写,并去掉所有不需要的大写。

我知道我想使用 ASCII 码和异或位来更改 ASCII 字符,然后转换为字母。这是因为十进制的位序列“00100000”32可以与逻辑运算符“XOR”一起使用来改变大小写。

8 位 ASCII 表示 大写字母“A”是 01000001。如果将其与 0010000 进行异或运算,您将得到 01100001,即小写字母“a”的 ASCII 码。

但我只能对字母执行此操作,所有特殊字符保持不变。

所以我认为我需要使用如下代码: xori $s3,$s3,32

不知道如何在我的程序中实现这一点。感谢您的帮助。

【问题讨论】:

    标签: assembly mips xor qtspim


    【解决方案1】:

    您需要两个名为 swap_case 的函数和另一个名为 end_of_sentence 的函数。

    这些对您有什么帮助?好吧,您需要在从文件中读取的每个字符上调用end_of_sentence。这应该做的是根据读取的字符是“.”、“?”、“!”、“\n”之一返回 1 还是 0。

    另一个函数只是接受一个字符并将大小写转换为另一种大小写。您使用它的方式是仅在字符大小写不正确时才调用它。

    我会带你开始使用第一个功能

    end_of_sentence:
      li $v0, 1
      beq $a0, '.', eos_ret
      beq $a0, '?', eos_ret
      beq $a0, '\n', eos_ret
      beq $a0, '!', eos_ret
      li $v0, 0    
      eos_ret: jr $ra
    

    您可以决定如何实现第二个功能。有很多方法。

    我觉得还有一个比较有用的就是这个

    isalpha:
      li $v0, 0
      blt $a0, 65, isalpha_ret
      bgt $a0, 122, isalpha_ret
      li $v0, 1
      bge $a0, 97, isalpha_ret
      ble $a0, 90, isalpha_ret
      li $v0, 0
      isalpha_ret: jr $ra
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多