Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
X86 assembly language
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==="Hello world!" program for Linux in AT&T and NASM assembly=== {|class=wikitable ! width=30%|AT&T (GNU as) !! width=30%|Intel (NASM) !! Description |- |<syntaxhighlight lang="gas"> .data </syntaxhighlight> |<syntaxhighlight lang="nasm"> section .data </syntaxhighlight> |Like in the Windows example, <code>.data</code> is the section for initialized data. |- |<syntaxhighlight lang="gas"> str: .ascii "Hello, world!\n" </syntaxhighlight> |<syntaxhighlight lang="nasm"> str: db 'Hello world!', 0Ah </syntaxhighlight> |Define a string of text containing "Hello, world!" and then a new line (<code>\n</code>, which is <code>0x0A</code>). Bind the label "str" to the address of the defined string. |- |<syntaxhighlight lang="gas"> str_len = . - str </syntaxhighlight> |<syntaxhighlight lang="nasm"> str_len: equ $ - str </syntaxhighlight> |Calculate the length of <code>str</code>. <code>.</code> means "here" in gas and <code>$</code> means the same in nasm. By subtracting "str" from "here", one gets the length of the previously-defined string. |- |<syntaxhighlight lang="gas"> .text </syntaxhighlight> |<syntaxhighlight lang=nasm> section .text </syntaxhighlight> |Like in the Windows example, <code>.text</code> is the section for program code. |- |<syntaxhighlight lang="gas">.globl _start</syntaxhighlight> |<syntaxhighlight lang=nasm>global _start</syntaxhighlight> |export the _start function to the global scope for it to be "seen" by the linker |- |<syntaxhighlight lang="gas">_start:</syntaxhighlight> |<syntaxhighlight lang="nasm">_start:</syntaxhighlight> |Define a label called <code>_start</code>, to which we will write our subroutine. The name <code>_start</code>, by Linux convention, defines the entry point. |- |<syntaxhighlight lang="gas"> movl $4, %eax movl $1, %ebx movl $str, %ecx movl $str_len, %edx </syntaxhighlight> |<syntaxhighlight lang="nasm"> mov eax, 4 mov ebx, 1 mov ecx, str mov edx, str_len </syntaxhighlight> |Prepare a system call. EAX=4 requests the "sys_write" call on Linux x86. EBX=1 means "stdout" for sys_write. ECX holds the string to write, and EDX holds the number of bytes to write. The is equivalent to the libc-wrapped version <code>write(1, str, str_len)</code>. |- |<syntaxhighlight lang="gas"> int $0x80</syntaxhighlight> |<syntaxhighlight lang="nasm"> int 80h</syntaxhighlight> |On x86, the system interrupt "80h" is used for invoking a system call according to the values of eax, ebx, ecx, and edx. |- |<syntaxhighlight lang="gas"> movl $1, %eax movl $0, %ebx int $0x80 </syntaxhighlight> |<syntaxhighlight lang="nasm"> mov eax, 1 mov ebx, 0 int 80h </syntaxhighlight> |Load another system call, then call it with INT 80h: EAX=1 is sys_exit, and EBX for sys_exit holds the return value. A return value of 0 means a normal exit. In C syntax, <code>_exit(0);</code>. |} Note for NASM: <pre> ; This program runs in 32-bit protected mode. ; build: nasm -f elf -F stabs name.asm ; link: ld -o name name.o ; ; In 64-bit long mode you can use 64-bit registers (e.g. rax instead of eax, rbx instead of ebx, etc.) ; Also change "-f elf " for "-f elf64" in build command. ; For 64-bit long mode, "lea rcx, str" would be the address of the message, note 64-bit register rcx. </pre>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)