Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use .globl main in MIPS assembly language?

       .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?

like image 446
Goodmorning Avatar asked Oct 11 '15 07:10

Goodmorning


People also ask

What does .globl main mean in MIPS?

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 .

What is .global 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.

What does .space mean in MIPS?

. 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.

What are labels in MIPS?

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.

What is .word in MIPS?

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.

What are the expectations of MIPS assemblers?

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.

How do labels work in MIPS?

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.

What is MIPS?

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.

What is global main in assembly language programming?

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.


2 Answers

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.

like image 195
NlightNFotis Avatar answered Sep 19 '22 07:09

NlightNFotis


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.