Author Topic: Global  (Read 754 times)

Offline JohnG

  • Jr. Member
  • *
  • Posts: 32
Global
« on: February 07, 2022, 03:08:00 AM »
Hi all,

Trying to find some examples using multiple Global directives.  I have one but it does not explain how it all goes together, and how it is saved (ie name and such) and where the programs actually go. Sounds simple enough but I only ever find 1 global.

John

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: Global
« Reply #1 on: February 07, 2022, 03:46:32 AM »
Dunno. I guess you could have more than one global per module. Can't recall seeing it. What happens when you try it?

Best,
Frank


Offline JohnG

  • Jr. Member
  • *
  • Posts: 32
Re: Global
« Reply #2 on: February 07, 2022, 03:54:43 AM »
Hi

This was part of the program example.  Connecting the two programs.




Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: Global
« Reply #3 on: February 07, 2022, 05:58:13 AM »
i John.

jpg? I see a picture of some text. No idea what to do with it. does not look like even one file, let alone two. I've tried to cook up an example but not having much luck with it. It would be for "imnux anyway. Possibly later? Any more information you can give?

Best,
Frank


Offline JohnG

  • Jr. Member
  • *
  • Posts: 32
Re: Global
« Reply #4 on: February 07, 2022, 06:25:17 AM »
hi

It was just part of the main program that includes the Global for the second program ( factorial ) showing how it was formatted.

I will do both separately and see what happens, even if it works it will probably not help in understanding what's going on.

John

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: Global
« Reply #5 on: February 08, 2022, 03:17:51 AM »
Hi John.

Let me run down what little I think I understand about "global" and the complimentary "extern". Neither of them generate any code. They instruct Nasm to put information in the linkable header for the use of the linker. "global" tells the linker "this function is here, in this module, in case some other module is looking for it". "extern" tells the linker, "this function is used here, but exists in some  other module. Go find it and fill in it's address here."

I think that's the essence of it.

Code: [Select]
global main
... would be typical. There's some C "startup code" which contains the line:
Code: [Select]
call main
This is to tell the linker where "main" is ("main" may be spelled with a leading underscore or trailing underscore or no underscore at all, depending on... whatever.)

Code: [Select]
global _start
would be another... but usually not both. The "_start" label - the entrypoint - is th the "startup
code" if it's used. We only want one!

Your example seems to be
Code: [Select]
global main
global factorial
if I understand it. "main" called from startup code and "factorial called from...? But I may npt be understanding it.

I am trying to cook up an example.
phrases.asm
Code: [Select]
global hello
global goodbye

section .text
hello:
; code to print "hello"
ret

goodbye:
;code to print "goodbye"
ret

Note no entrypoint or exit.

To be called from:
chat.asm
Code: [Select]
global _start
extern hello
extern goodbye

section .text
_start:
call hello
call goodbye
;exit cleanly

however, it's not going well...

I don't know if that's any help.

Best,
Frank



Offline JohnG

  • Jr. Member
  • *
  • Posts: 32
Re: Global
« Reply #6 on: February 08, 2022, 03:42:59 AM »
Hi Frank,

Thanks for trying to assist.

I think I was getting confused a little as I had not really covered Functions as yet.

I found a definition of a Function which may explain my issue.
It was the 2 Global directives that were confusing me, but the def explains it.

global <funcName>
<funcName>:
        ; function body
ret

And this is all within the same program, which was not explained clearly in the tut I was looking at. And is what you are showing me in your example.

John

Offline JohnG

  • Jr. Member
  • *
  • Posts: 32
Re: Global
« Reply #7 on: February 08, 2022, 03:48:28 AM »
Hi ,

And as found by another member, if using GoLink you do not need to use the global directive, it works it out for you.  But I will stick with it for the moment.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: Global
« Reply #8 on: February 08, 2022, 05:08:38 AM »
Okay, John, glad you got it working.

After cleaning up a few stupid typos, I got my example working, too. So all is good.

Best,
Frank


Offline JohnG

  • Jr. Member
  • *
  • Posts: 32
Re: Global
« Reply #9 on: February 08, 2022, 06:40:53 AM »
Hi,

I will also give your example a try, as I am sure it will be good to know at some point.

John

Offline JohnG

  • Jr. Member
  • *
  • Posts: 32
Re: Global
« Reply #10 on: February 08, 2022, 12:03:03 PM »
Hi Frank

I tried your example and came up with this.

phrases.asm

Code: [Select]
section .text

    global hello
    global goodbye
   
hello:
    mov     rax, 0x14
    ret
   
goodbye:
    mov     rcx, 0x1e
    ret


chat.asm

Code: [Select]
global start

%include  'phrases.asm'


section .text

start:
    call    hello
    call    goodbye


It works fine as shown on a debugger.
« Last Edit: February 08, 2022, 09:16:40 PM by JohnG »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: Global
« Reply #11 on: February 08, 2022, 11:38:37 PM »
Okay, This is very(?) different because of the "$include". This is the same as cut and pasting the two files into one, and should work without either "global" or "extern
.
What I did, and I should have shpwed this(!), was:
Code: [Select]
nasm -f elf64 phrases.asm
nasm =f elf64 chat.asm
ld chat.o phrases,o =o chat
this is my understanding of what :global" and "extern" do.

So perhaps you should try something like that - -f win64I guess? - before we claim to have fotten it figured out...

I should try to make a "sticky" post on how to ask for hel[. It shuld point out that Nasm will do a lot of things, so you should tell us the command line you gave to Nasm and the commans libe you gave to the linker, if any. I should do the same!

Best,
Frank


Offline JohnG

  • Jr. Member
  • *
  • Posts: 32
Re: Global
« Reply #12 on: February 09, 2022, 12:46:05 AM »
Hi,

OK, think I have it now I used,

Code: [Select]
nasm -f win64 phrases.asm
nasm -f win64 chat.asm
golink /entry: start chat.obj  phrases.obj msvcrt.dll kernel32.dll user32.dll

works fine on the debugger.

John