【问题标题】:Mips char array indexing with counterMips char数组索引与计数器
【发布时间】:2013-09-17 06:57:46
【问题描述】:

程序概述:输入一个数字1-26,也给对应的大写字母数字

我的逻辑:使用 .byte 和字符设置一个数组类型“结构”。有一个计数器通过数组。一旦计数器与输入数字相等,就打印出数组中的当前点。

这是一个家庭作业,所以我不是试图“轻推”答案,但指导会很有帮助。

这就是我认为哪里出错的原因。当我将 1 添加到地址时,由于某种原因它给了我一个错误。但是当我添加 4 时它工作正常吗?一个字符应该只需要 1 位正确吗?我知道当索引数组中整数的地址时,它应该是 4。

    .data
prompt: .asciiz "Enter the value of n here: "
larray: .byte 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
    .globl main
    .text

main:
    la  $t1, larray # Loads Array addresss into $t1
    li  $v0, 4      # System call for Print String
    la  $a0, prompt # Load address of prompt into $a0
    syscall         # Print the prompt message
    li  $v0, 5      # System call code for Read Integer
    syscall         # Read N into $v0
    move    $t3, $v0
    li  $t0, 0      # Loads int 0 into $t0



loop:
    beq     $t0, $t3, end   # Break
    lw  $a0, ($t1)  # Loads current pointer


    addi    $t0, $t0, 1 # Adds one to $t0 (Counting up +1)
    addi    $t1, $t1, 1 # Advances to next address in the array

    j   loop


end:
    li  $v0, 11     # Print at current index
    syscall

    li  $v0, 10     # return control to system
    syscall

我确实研究过如何以不同的方式访问 char,但我认为我不能以这种方式实现,因为您需要硬编码? 关联: This is the Stack Link that I found

【问题讨论】:

    标签: arrays assembly mips spim


    【解决方案1】:

    不需要数组或循环。如果您只想找到 1-26 范围内单个数字的对应大写字母,这就足够了:

    li  $v0, 5       # System call code for Read Integer
    syscall          # Read N into $v0
    addiu $v0,$v0,'A'-1  # Convert the number 1-26 to the character 'A'-'Z'
    

    一个字符应该只占用 1 位正确吗?

    一个字节

    当我将 1 添加到地址时,由于某种原因它给了我一个错误。但是当我添加 4 时它工作正常吗?

    您正在使用lw 指令,该指令加载一个(4 个字节)。要加载单个字节,请使用 lblbu 指令(lb 用于有符号字节,lbu 用于无符号字节)。

    【讨论】:

    • 感谢您提供信息丰富的回复!我没有意识到这两件事,这将对我将来有所帮助,再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    相关资源
    最近更新 更多