1

I have a simple project to do and I think it's working fine, but I have these problems:

Even though I have declare this:

.data
.msg:.asciiz "Give a number: " <-
.text2:.asciiz "x==-5"
.text1:.asciiz "Array is full"

I get a message saying: The following symbols are undefined: msg

And in the others messages text2 and text1 when they executed I'm getting weird symbols back. Why?

This is my code:

.data
.msg:.asciiz "Give a number: "  #messages  <-UNDEFINED MESSAGE
.text2:.asciiz "You typed -5"
.text1:.asciiz "Array is full"

.align 2
a:.space 20  #array for 5 integers

.text
.globl main
.globl loop
.globl loop2
.globl end1
.globl end2

main:
addi $14,$0,5      #t=5

addi $15,$0,-5  #f=-5
addi $16,$0,0  #i=0
addi $17,$0,0 #x=0
la $18,a

addi $2,$0,4   #Give a number:
la $4,msg
syscall

loop: #for
beq $16,$14,end1  #if i==5, end

addi $2,$0,5  #take number from console
syscall
add $17,$2,$0  #add it to array
sw $17, 0($18)

addi $18,$18,4 #change array
beq $17,$15,end2 #if x==-5, end
bne $16,$14,loop2 #if i!=5, go to loop2

loop2:
addi $16,$16,1 #i++
addi $2,$0,4   #Give a number:
la $4,msg
syscall
j loop  #jumb to first loop

end1:  #if i==5
addi $2,$0,4  #"Array is full"
la $4,text1
syscall

end2:  #if x==-5
addi $2,$0,4 #"You typed -5"
la $4,text2
syscall
Cœur
  • 37,241
  • 25
  • 195
  • 267
jpdev
  • 15
  • 4
  • 1
    What if you use `msg` instead of `.msg` ? – Michael May 18 '14 at 14:21
  • yeah thank you :).. first problem solved! but can you explain to me why did I have to use msg without a dot ? Also the next messages (text1,text2) still appear weird or sometimes they don't appear at all. Can somebody help me with that ? :) – jpdev May 18 '14 at 19:31
  • Is this actually the code you're running? It doesn't make sense - you have strings and labels named "text1" and "text2", and in the code under those labels you load "end1" and "end2" before the syscall. – nobody May 18 '14 at 19:55
  • i think i fixed the code :).. it runs good in spim now. thank you :) – jpdev May 23 '14 at 16:14

1 Answers1

0

.msg is a different label from msg. You define .msg with .msg: on an early line, but then reference msg with a la $4,msg. Those are two different symbols, so the assembler complains that msg was never defined.

The leading . is special in .globl; it's not a label at all but rather a directive to make another symbol global. See https://sourceware.org/binutils/docs/as/Global.html

Similarly, .data doesn't declare a label called data, it's a shortcut for .section .data. And yes, the section name really does have a . in it.

You can define "local" label names that won't show up in the object file by using label names like .Lmsg. https://sourceware.org/binutils/docs/as/Symbol-Names.html#index-local-symbol-names

C compiler output typically uses this, you'll find a lot of .L1:, .L2: and .LC0 type of labels in gcc -S output. (How to remove "noise" from GCC/clang assembly output?)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847