-1

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
rue02
  • 1
  • 1

1 Answers1

1

"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).

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53