assemblymacrosmipsmars-simulator

What is the effect of the assembler directive `.global`


When I run my code in MARS it doesn't need any .global directive and i know that MARS doesn't use that directive.

Now when i ported my code to a particular assembler (MCB32) which runs from the command line. So I was have to modify my code and added those directive which you can see in the code sniped.

So what is the effect of that directive? Is that mean something like this function is defined global or so?

The.macro I used in MARS assembler.

 # Written 2015 by F Lundevall
 # Copyright abandonded - this file is in the public domain.

.macro  PUSH (%reg)
    addi    $sp,$sp,-4
    sw  %reg,0($sp)
 .end_macro

.macro  POP (%reg)
    lw  %reg,0($sp)
    addi    $sp,$sp,4
.end_macro
.data
.align 2
 main:

The .macro I used in MCB32 assembler.


.global delay .global time2string

.macro PUSH reg addi $sp,$sp,-4 sw \reg,0($sp) .endm .macro POP reg lw \reg,0($sp) addi $sp,$sp,4 .endm

.data .align 2 .text main:


Solution

  • well assuming you actually used those labels somewhere then it would make sense. I dont see them in the code you provided.

    In one/some assemblers all labels are local unless otherwise specified. In C for example all function names are global unless you put a static out front (that might be a standard thing or a convention that compilers use, dont know, dont beat me up over that). So for C you would generally ADD a static out front if you dont want to publish that function outside that object. In asm at least for one/some you have to declare the label global otherwise other objects cant link up with it. How that is declared is sometimes in various ways even within one assembler's assembly language, sometimes you have directives to declare something a function with parameters, etc or you just say this label is global.

    Unlike many other languages assembly language is generally not defined by some standard, at best the documentation from the chip or IP vendor, but we have seen countless times how that has failed to define an assembly language. The assembler the program that reads and parses the assembly language basically defines that assembly language, you can have as many assemblers as there are programmers for the same target or platform. so long as the machine code is right which is very much governed by a standard, hardwired in the logic itself, you can invent pretty much anything you want to allow someone to write programs that make machine code for that target. Generally but not always, the spirit of the chip or IP vendor that created the machine language's documenation is used for the instructions, but the directives, macro language, etc are often very much in the realm of the assembler, sometimes trying to conform to a prior vendors product, sometimes going out on their own with their own thing.

    So you really have to go assembler by assembler and look at the documentation if available, and dont assume that assembly language for one target machine/processor ports from assembler to assembler, expect to have to re-write portions, if you dont then good for you, you got lucky.