繁体   English   中英

用计数器欺骗char数组索引

[英]Mips char array indexing with counter

程序概述:输入数字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,但是我认为我无法实现这种方式,因为您将需要对其进行硬编码? 链接: 这是我找到的堆栈链接

不需要数组或循环。 如果您只想找到一个对应的大写字母,范围在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表示无符号字节)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM