0

Overview of program: Input of a number 1-26, give the corresponding Capital letter too the number

My logic: Set up an Array type "structure" using .byte with chars. have a counter going through the array. once the counter is equivenlt to the input number print out the current point at the array.

This is a homework assignment so I'm not trying to "nudge" out the answer but guidance would be very helpful.

This is why where I think its going wrong. When I add 1 to the address it for some reason gives me a error. But when i add 4 it works fine? A char is supposed to take only 1 bit correct? I understand that when indexing address's of ints in an array it should be by 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

I did do research on how to access the char a different way, but I don't think I can implement this way because you would need it hard coded? Link: This is the Stack Link that I found

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Conor
  • 640
  • 5
  • 10
  • 21

1 Answers1

1

There's no need for the array or the loop. If all you want to do is find the corresponding capital letter for a single number in the range 1-26, this would suffice:

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'

A char is supposed to take only 1 bit correct?

One byte.

When I add 1 to the address it for some reason gives me a error. But when i add 4 it works fine?

You're using the lw instruction, which loads one word (4 bytes). To load a single byte, use the lb or lbu instruction (lb is for signed bytes, lbu for unsigned).

Michael
  • 57,169
  • 9
  • 80
  • 125
  • Thank you for the informative response! I didn't realize those two things, which will help me in the future, thanks again. – Conor Sep 17 '13 at 07:21