mips装配未定义和奇怪的符号

时间:2014-05-18 13:20:50

标签: undefined mips

我有一个简单的项目要做,我认为它工作正常,但我有这些问题:

即使我已声明:

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

我收到一条消息说:以下符号未定义:msg

在其他消息中,text2和text1在执行时我得到了奇怪的符号。 为什么?

这是我的代码:

.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

1 个答案:

答案 0 :(得分:0)

.msg是与msg不同的标签。您可以在早期行中使用.msg定义.msg:,然后使用msg引用la $4,msg。这是两个不同的符号,因此汇编程序抱怨msg从未定义过。

领先的..globl中很特别;它根本不是标签,而是指令,以使另一个符号成为全局符号。见https://sourceware.org/binutils/docs/as/Global.html

同样,.data没有声明名为data的标签,它是.section .data的快捷方式。是的,部分名称确实包含.

您可以定义&#34; local&#34;使用.Lmsg等标签名称不会在目标文件中显示的标签名称。 https://sourceware.org/binutils/docs/as/Symbol-Names.html#index-local-symbol-names

C编译器输出通常使用此功能,您可以在.L1:输出中找到大量.L2:.LC0gcc -S类型的标签。 (How to remove "noise" from GCC/clang assembly output?