1

I want to change a character in a string. For that I load in $t2 the char which is representing a hit. In this example the " " char at the 7th position should change to a "x". After trying to updating my board with sh $t2, 0($t0) I get the following errors:

Exception occurred at PC=0x0040003c
Unaligned adress in store: 0x1001000f

I guess I cannot update the board with the command I used, could you tell me how to do it right? Thank you in advance :)

 # data segment
        .data
        hit:      .asciiz   "-"
        ship:     .asciiz   "x"
        miss:     .asciiz   "o"
        water:    .asciiz   " "

        board:          .asciiz     "    x xx   x  x      x  x  xxx x  x         x   x         x xxxx  x x       x   xx    x         x xx"

        .text
        .globl main

    main:

        la $t0, board

        # this is position of the array which should be a "x" char at the moment
        addi $t0, $t0, 7

        la $t1, hit
        lhu $t2, 0($t1)     # loading the hit char "-"
        sh $t2, 0($t0)      # ERROR : Here I tried to update the board


        li $v0, 10
        syscall
Richie
  • 73
  • 1
  • 6

1 Answers1

1

You need to use sb to store a byte. sh stores a half-word and must be two-byte aligned.

prl
  • 11,716
  • 2
  • 13
  • 31
  • seems so obvious, could you explain me why I need to load half word in $t2 even though it is a character which is 1 byte long? – Richie Mar 30 '18 at 16:08
  • You shouldn’t. You can use lb to load a byte from memory. But what you should really do is load an immediate. Unfortunately, I don’t know the MIPS instruction set, so I don’t know how to do that. I think you use ori. – prl Mar 30 '18 at 16:16
  • ohh yes that makes sense. I guess I got kinda confused because lhu worked and it only put 1 byte in my register instead of 2. Do you know why mips did that? Is that maybe the case because after I declare in data segment a string mips puts at the and 00 which means null and I read for example 0078 which would be the half word but in register it cut out the 00 because it isnt needed – Richie Mar 30 '18 at 16:27
  • Yes, that’s right. It didn’t “cut out” the 00; it loaded it into bits 15:8 of the register. If you had used ascii instead of asciz, it would have put “x” in those bits. – prl Mar 30 '18 at 16:31
  • @prl: use `li $t1, constant`. It's a pseudo-instruction that uses 1 or 2 instructions as necessary to get a constant into a register. `lui` and/or `ori`. (or `addiu`) https://stackoverflow.com/questions/49550512/confusion-about-load-word-lw-vs-load-addressla-and-offsets-in-mips-assembly/49552715?noredirect=1#comment86164053_49552715 – Peter Cordes Mar 30 '18 at 18:06