什么是汇编语言的全球_start? [英] What is global _start in assembly language?

查看:520
本文介绍了什么是汇编语言的全球_start?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的汇编级code ...

  .text段
全球_start
_start:MOV EAX,4
        MOV EBX,1
        MOV ECX,MESG
        MOV EDX,大小
        INT 0x80的
退出:MOV EAX,1
        INT 0x80的
段.data
MESG DB'金刚',为0xA
大小EQU $ -mesg

输出:

 根@ BT:〜/竞技场#NASM -f精灵a.asm -o a.o
根@ BT:〜/竞技场#LD -o出来a.o
根@ BT:〜/竞技场#./out
金刚

我的问题的使用何种全球_start为?我想我的运气Mr.Google,我发现它被用来告诉我的程序的起点。为什么不能,我们只是有 _start 来告诉程序开始像下面这给出的,其中产生还挺
屏幕上的警告

  .text段
_start:MOV EAX,4
        MOV EBX,1
        MOV ECX,MESG
        MOV EDX,大小
        INT 0x80的
退出:MOV EAX,1
        INT 0x80的
段.data
MESG DB'金刚',为0xA
大小EQU $ -mesg根@ BT:〜/竞技场#NASM -f精灵a.asm
根@ BT:〜/竞技场#LD -e _start -o出来a.o
LD:警告:无法找到入口符号_start;默认为0000000008048080
根@ BT:〜/竞技场#LD -o出来a.o
LD:警告:无法找到入口符号_start;默认为0000000008048080


解决方案

全球指令是NASM具体。它是在你的code出口符号,它在生成的目标code点。在这里,您标记 _start 符号全球化,所以它的名字在对象$ C $下加入( a.o )。链接器( LD ),可以让它知道在哪里,以纪念在输出可执行文件的入口点读取对象code的象征,它的价值。当您运行可执行程序,它开始于凡在code标记为 _start

如果一个全球指令缺少一个符号,符号将不会被放置在目标code出口表,以便链接器就无法知道该符号的方式。

如果您想使用 A不同的入口点名称_start (这是默认的),您可以指定 -e parametre到LD,如:

  LD -e my_entry_point -o出来a.o

This is my assembly level code ...

section .text
global _start
_start: mov eax, 4
        mov ebx, 1
        mov ecx, mesg
        mov edx, size
        int 0x80
exit:   mov eax, 1
        int 0x80
section .data
mesg    db      'KingKong',0xa
size    equ     $-mesg

Output:

root@bt:~/Arena# nasm -f elf a.asm -o a.o
root@bt:~/Arena# ld -o out a.o
root@bt:~/Arena# ./out 
KingKong

My question is What is the global _start used for? I tried my luck with Mr.Google and I found that it is used to tell the starting point of my program. Why cant we just have the _start to tell where the program starts like the one given below which produces a kinda warning on the screen

section .text
_start: mov eax, 4
        mov ebx, 1
        mov ecx, mesg
        mov edx, size
        int 0x80
exit:   mov eax, 1
        int 0x80
section .data
mesg    db      'KingKong',0xa
size    equ     $-mesg

root@bt:~/Arena# nasm -f elf a.asm
root@bt:~/Arena# ld -e _start -o out a.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000008048080
root@bt:~/Arena# ld -o out a.o
ld: warning: cannot find entry symbol _start; defaulting to 0000000008048080

解决方案

global directive is NASM specific. It is for exporting symbols in your code to where it points in the object code generated. Here you mark _start symbol global so its name is added in the object code (a.o). The linker (ld) can read that symbol in the object code and its value so it knows where to mark as an entry point in the output executable. When you run the executable it starts at where marked as _start in the code.

If a global directive missing for a symbol that symbol will not be placed in the object code's export table so linker has no way of knowing about the symbol.

If you want to use a different entry point name than _start (which is the default), you can specify -e parametre to ld like:

ld -e my_entry_point -o out a.o

这篇关于什么是汇编语言的全球_start?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆