如何使用用户输入创建一个范围在0-100之间的包含8个元素的数组?

huangapple go评论77阅读模式
英文:

How Do I Use User Input To Make An Array Of 8 With Ranges 0-100?

问题

需要在LC3中制作一个冒泡排序程序,该程序接受用户输入(8个范围在0-100之间的数字)并按升序排序它们。到目前为止,以下是我所拥有的(要求用户输入),但我一直收到一个错误消息,上面写着:

"> "立即字段超出范围:预期值适合5位(即在-16到15之间,包括-16和15),但找到了-100"

有谁可以帮我修复它?

.ORIG x3000
LEA R0, PROMPT ; 显示提示
PUTS
ADD R0, R0, #0 ; 数组基址
ADD R1, R1, #8 ; 计数器

GET_INPUT

GETC

OUT ; 回显字符
; 将字符转换为数字
LD R2, ASCII_ZERO ; ASCII '0' = 48
NOT R2, R2
ADD R2, R2, #1
ADD R3, R2, R0 ; 计算数组元素14的地址
LDR R4, R3, #0 ; 加载现有值
ADD R4, R4, R2 ; 添加新的数字

; 检查数字是否在范围内
ADD R5, R4, #-100 ; 检查是否小于-100
BRn INVALID_INPUT
ADD R5, R4, #100 ; 检查是否大于100
BRz INVALID_INPUT

STR R4, R3, #0 ; 存储新值

BR NZP, GET_INPUT ; 重复直到输入28个数字为止

END_LOOP
HALT

INVALID_INPUT
LEA R0, ERROR_MSG ; 显示错误消息
PUTS
BR GET_INPUT

PROMPT .STRINGZ "输入一个数字(0-100):"
ERROR_MSG .STRINGZ "无效的输入!请输入0到100之间的数字。"
ASCII_ZERO .FILL x0030

.END
英文:

I need to make a bubble sort program in LC3 that takes user input (8 numbers with ranges 0-100)and sorts them in ascending order. This is what I have so far (asking the user for input) but I keep getting an error that says:;

> "immediate field is out of range: expected value to fit in 5 bits (i.e., to be between -16 and 15, inclusive), but found -100"

Can anyone help me fix it?

.ORIG x3000
LEA R0, PROMPT ; Display prompt
PUTS
ADD R0, R0, #0 ; Array base address
ADD R1, R1, #8 ; Counter


GET_INPUT

GETC

OUT ;Echo the character
 ;Convert the character to a number
 LD R2, ASCII_ZERO ;ASCII '0' = 48
 NOT R2, R2
 ADD R2, R2, #1
ADD R3, R2, R0 ; Calculate array element 14 address
 LDR R4, R3, #0 ; Load existing value
 ADD R4, R4, R2; Add new digit

 ; Check if the number is within the range
 ADD R5, R4, #-100 ; Check if < -100
 BRN INVALID_INPUT
 ADD R5, R4, #100 ; Check if > 100
 BRZ INVALID_INPUT

STR R4, R3, #0 ; Store the new value

BR NZP, GET_INPUT ; Repeat until 28 numbers 26 are entered

 END_LOOP
 HALT

 INVALID_INPUT
 LEA R0, ERROR_MSG ; Display error message
 PUTS
 BR GET INPUT

 PROMPT .STRINGZ  "Enter a number (0-100):"
ERROR_MSG .STRINGZ "Invalid input! Please  enter a number between 0 and 100."
 ASCII_ZERO .FILL x0030

.END

答案1

得分: 1

> "立即字段超出范围:期望值适合5位(即在-16和15之间,包括边界),但发现-100"

当一个立即值无法适应指令时,您可以将立即值构建到另一个寄存器中,并使用违规指令的非立即形式。

  • 您可以使用多条指令来构建立即值—使用这种方法,您可以构建任何大小的立即值,但根据所需立即值的大小,可能需要很多指令。

例如,要加载值64,您可以加载31,然后将其加倍以获得62,然后再加2:

ANDI R0, R0, #0    # 清除R0
ADDI R0, R0, #31
ADD  R0, R0, R0    # R0=R0*2,所以是62
ADDI R0, R0, #2    # R0 = 62+2 = 64
  • 您可以从存储器中加载一个具有立即值的字—使用这种方法,您可以加载一个完整字大小(16位)的立即值。
LD R0, P64
...
...
P64 .FILL #64

这两种形式都将立即值放入寄存器中,以便与另一条指令一起使用作为寄存器操作数。

因此,而不是ADDI R4, R4, #-100,您可以执行LD R5, M100; ADD R4, R4, R5,其中M100是一个带有数据字-100的数据标签声明:M100 .FILL #-100。  显然,这种方法需要一个短暂的空闲寄存器(这里是R5,但根据您的需求选择一个临时可用的寄存器)。

英文:

> "immediate field is out of range: expected value to fit in 5 bits (i.e., to be between -16 and 15, inclusive), but found -100"

When an immediate doesn't fit within the instruction, then you can build the immediate into another register and use a non-immediate form of the offending instruction.

  • You can build the immediate using several instructions — using this approach you can build immediates of any size, but it will might require many instructions, depending on the value of the immediate you want.

    For example, to load the value 64, you can load 31, then add it to itself to accomplish 62, and then add 2 more:

    ANDI R0, R0, #0    # clear R0
    ADDI R0, R0, #31
    ADD  R0, R0, R0    # R0=R0*2, so 62
    ADDI R0, R0, #2    # R0 = 62+2 = 64
  • You can load a word from memory that has the immediate value — using this approach you can load an immediate of full word size (16 bits).
    LD R0, P64
    ...
    ...
P64 .FILL #64

Either of these forms will put an immediate into a register for use with another instruction as register operand.

So, instead of ADDI R4, R4, #-100, you can do LD R5, M100; ADD R4, R4, R5, where M100 is a data label declared with a data word of -100: M100 .FILL #-100.  Obviously, this approach requires a spare register for a short duration (here R5, but choose one for your purposes that is temporarily free).

huangapple
  • 本文由 发表于 2023年5月29日 10:29:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定