NASM Assembler, how to define label twice?

﹥>﹥吖頭↗ 提交于 2020-05-23 10:53:54

问题


I have different "*.asm" files that need to be included in the "main.asm" file. The problem I'm facing is that: In many files I have declared labels like "loop", "forLoop", "whileTag" etc... in the same way ( i.e. with the same name ) And when I try to %include "file1.asm" and %include "file2.asm" it gives me a compilation error. It says that I can't declare the same label twice ( i.e. file1.asm and file2.asm, both have "loopHere" label declared ). How do I solve this ? Thanks

The problem with local labels is: Say I have

File 1:

.label1
;staff

Now file 2:

;code that uses label1
.label1 ; definition after usage

Now if I:

%include "file1.asm"
%include "file2.asm"

The resulting main.asm would be:

.label1
;staff
;code that uses label1
.label1 ; definition after usage

Code at line 3 would actually use label1 at line one and not the one at line 4

Quote from NASM Manual

A label beginning with a single period is treated as a local label, which means that it is associated with the previous non-local label.

My bad, I just realized that if I:

File 1:

file1: ; add this label
.label1
;staff

Now file 2:

file2: ; add this label
;code that uses label1
.label1 ; definition after usage

Everything works great!

Access them with:

file1.label1
file2.label1

回答1:


With local labels. Local labels start with a dot.

Someproc:
.Somelabel:
Ret

Anotherproc:
.Somelabel:
Ret

They are visible to the proc they are in. You can access them from anywhere by prefixing them with the proc name.

Someproc:
.Somelabel:
Ret

Anotherproc:
.Somelabel:
jmp Someproc.Somelabel


来源:https://stackoverflow.com/questions/26979005/nasm-assembler-how-to-define-label-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!