.text
.globl main
.ent main
I don't know what .globl
and .ent
do.
What is the role?
Do I need to use globl. main
and .ent main
all the time?
In your case, . globl is an assembler directive that tells the assembler that the main symbol will be accessible from outside the current file (that is, it can be referenced from other files), and . ent is a debugger (pseudo) operation that marks the entry of main .
global main basically means that the symbol should be visible to the linker because other object files will use it. Without it, the symbol main is considered local to the object file it's assembled to, and will not appear after the assembly file is assembled.
. space Len directive instructs the assembler to reserve Len bytes. As every word has 4 bytes, when Len is 20 you are instructing the assembler to reserve 5 words.
In MIPS assembly, a label is simply a string used to name a location in memory. A label may refer to the location of a data value (variable) or of an instruction. In essence, think of a label as representing an address.
A word generally means the number of bits that can be transferred at one time on the data bus, and stored in a register. In the case of MIPS, a word is 32 bits, that is, 4 bytes. Words are always stored in consecutive bytes, starting with an address that is divisible by 4.
From this, you can see a few of the expectations that most MIPS assemblers and simulators have: Comments begin with a hash symbol (#) and continue to the end of the line. Variables are declared in the .data segment, and assembly language code must be in the .text segment. Labels work as with in other versions of assembly language.
Labels work as with in other versions of assembly language. Variables are values used by a running program that can be changed at any time. In MIPS assembly language, allocating space for variables must be done in the .data segment, and generally requires you to specify the data type to be used.
MIPS Assembly Language Guide MIPS is an example of a Reduced Instruction Set Computer (RISC) which was designed for easy instruction pipelining. MIPS has a “Load/Store” architecture since all instructions (other than the load and store instructions) must use register operands.
i am using this site as a reference to Assembly language programming. i can analyse that main refers to the function main, but i don't understand the use of the global keyword... thank you in advance... Show activity on this post. global main basically means that the symbol should be visible to the linker because other object files will use it.
In your case, .globl
is an assembler directive that tells the assembler that the main
symbol will be accessible from outside the current file (that is, it can be referenced from other files), and .ent
is a debugger (pseudo) operation that marks the entry of main
.
It is going to be the same in any other ISA on the GNU GAS assembler, e.g. on x86_64 Linux:
main.S
.text
.global _start
_start:
/* exit syscall */
mov $60, %rax
mov exit_status, %rdi
syscall
exit_status.S
.data
.global exit_status
exit_status:
.quad 42
Assemble and run:
as -o main.o main.S
as -o exit_status.o exit_status.S
ls -o main.out exit_statis.o main.o
./main.out
echo $?
gives:
42
But if we remove the line:
.global exit_status
then ld
fails with:
main.o: In function `_start':
(.text+0xb): undefined reference to `exit_status'
because it cannot see the symbol exit_status
that it needs.
.globl
and .global
are synonyms as mentioned in the documentation: https://sourceware.org/binutils/docs/as/Global.html#Global so I just prefer to use the one with the correct spelling ;-)
We can observe what is going on by looking into the information contained in the ELF object files.
For the correct program:
nm hello_world.o mystring.o
gives:
main.o:
0000000000000000 T _start
U exit_status
exit_status.o:
0000000000000000 D exit_status
and for the failing one:
exit_status.o:
0000000000000000 d exit_status
And:
man nm
contains:
The symbol type. At least the following types are used; others are, as well, depending on the object file format. If lowercase, the symbol is usually local; if uppercase, the symbol is global (external). There are however a few lowercase symbols that are shown for special global symbols ("u", "v" and "w").
"D" "d" The symbol is in the initialized data section. "T" "t" The symbol is in the text (code) section. "U" The symbol is undefined.
On the C level, you can control symbol visibility with the static
keyword: What does "static" mean in C?
Tested in Ubuntu 16.04, Binutils 2.26.1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With