2

The programs runs with expected output but with the errors below

Code

.data

.text
.globl main
main: 
  addi $t0, $t0, 0    # i = 0
  addi $t2, $0, 10    # n = 10
  j WhileCond         # goto WhileCond

  WhileLoop:
    li $v0, 1         # print_int
    move $a0, $t0     # $a0 = i (print i)
    addi $t0, $t0, 1
    syscall

  WhileCond:
    blt $t0, $t2, WhileLoop # if i < 10 goto WhileLoop

  j $ra

User Text Segment

[00400000] 21080000  addi $8, $8, 0           ; 10: addi $t0, $t0, 0 # i = 0 
[00400004] 200a000a  addi $10, $0, 10         ; 11: addi $t2, $0, 10 # n = 10 
[00400008] 08100007  j 0x0040001c [WhileCond] ; 12: j WhileCond # goto WhileCond 
[0040000c] 34020001  ori $2, $0, 1            ; 15: li $v0, 1 # print_int 
[00400010] 00082021  addu $4, $0, $8          ; 16: move $a0, $t0 # $a0 = i (print i) 
[00400014] 21080001  addi $8, $8, 1           ; 17: addi $t0, $t0, 1 
[00400018] 0000000c  syscall                  ; 18: syscall 
[0040001c] 010a082a  slt $1, $8, $10          ; 21: blt $t0, $t2, WhileLoop # if i 
[00400020] 1420fffb  bne $1, $0, -20 [WhileLoop-0x00400020] 
[00400024] 03e00008  jr $31                   ; 23: j $ra 

Error

Exception occurred at PC=0x00000000
  Bad address in text read: 0x00000000
Instruction references undefined symbol at 0x800001dc
  [0x800001dc] 0x143a0000  bne $1, $26, 0 [ok_pc-0x800001d8]
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • 1
    That won't even assemble. *j $ra* is not a valid MIPS instruction. You mean *jr $ra*. – m0skit0 Oct 19 '11 at 12:11
  • 1
    With *jr $ra* it runs fine on MARS simulator (except that $ra has no assigned value and crashes when jumping to $ra). Which is the reason I guess you get this error: Exception occurred at PC=0x00000000 Bad address in text read: 0x00000000 – m0skit0 Oct 19 '11 at 14:03
  • Is that line required actually? To return from the main method? – Jiew Meng Oct 20 '11 at 08:21
  • You have no function call, so that line is not required in your case. In case you want to use this code as a function, then that line would be required, along with a few others like function entry/exit protocol (more info on the MIPS ABI). Also I would not use *j WhileCond* but rather *b WhileCond*. Jumps are usually used for far addresses, branches are for near addresses. AFAIK branches execute faster. – m0skit0 Oct 20 '11 at 10:00
  • @m0skit0, I don't think theres a `b` command. I think `beq $0, $0, WhileCond` will work tho – Jiew Meng Oct 20 '11 at 10:21
  • There's a *b* **instruction**, at least on MIPS IV. Check your MIPS documentation. – m0skit0 Oct 20 '11 at 10:26
  • The `b` instruction is an assembler shortcut for `beq $0,$0,offset`. MIPS has a lot of assembler shortcuts. – markgz Oct 20 '11 at 17:47

1 Answers1

3
addi $t0, $t0, 0    # i = 0

Won't set $t0 to 0 but rather leave it unchanged. (move $t0, $zero) or (addi $t0, $0, 0) would both work.

Also are you sure that $t0 and $t2 are preserved in the syscall?

Lastly the error you posted doesn't appear to occur in your code. Do you have a stack trace/dump of registers or anything else that might help in tracking it down?

user786653
  • 29,780
  • 4
  • 43
  • 53
  • Its working now (it appears `j $ra` is causing the problems ... tho I think @m0skit0, is right, it should not work at all ...). I remember syscall might not preserve some variables ... but it worked so far ... how might I fix this potential problem? – Jiew Meng Oct 19 '11 at 14:32
  • By saving it on the stack, it's all explained in [here](http://spimsimulator.sourceforge.net/HP_AppA.pdf). And it's very likely @m0skit0 got it right, I don't recall SPIMs starting conditions. – user786653 Oct 19 '11 at 14:43