Last active
August 26, 2023 16:11
Listing 3 from http://www.ibm.com/developerworks/library/l-gas-nasm/ translated to GNU as and written using intel syntax.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example adapted from http://www.ibm.com/developerworks/library/l-gas-nasm/ | |
# Assemble with: as --gstabs+ -o nasm2gas3.o nasm2gas3.s | |
# Link with: ld -o nasm2gas3 nasm2gas3.o | |
# | |
# Support intel syntal vs. ATT and don't use % before register names | |
.intel_syntax noprefix | |
.section .rodata | |
prompt_str: .ascii "Enter Your Name: \n" | |
.set STR_SIZE, . - prompt_str | |
greet_str: .ascii "Hello " | |
.set GSTR_SIZE, . - greet_str | |
.section .bss | |
# Reserve 32 bytes of memory | |
.lcomm buff, 32 | |
# The somewhat mysterious OFFSET FLAT: incantation tells the assembler to figure | |
# out the (4-byte) address where the variable x will end up when the program is | |
# loaded. Even the assembler does not have all the information to figure this | |
# out, since a program may be in several pieces and the assembler does not know | |
# where each piece will go. It is up to the loader to figure this out, so in fact | |
# all the assembler does with the OFFSET FLAT: reference is to make a note in the | |
# object file and it is the loader that will finally fill in the right value in | |
# the generated instruction. This is one of the respects in which object code | |
# (which ends up in a .o file after assembly) is not pure machine code. | |
# | |
# So | |
# mov ecx, OFFSET FLAT:\str | |
# | |
# and | |
# lea ecx, \str | |
# | |
# will do the same thing. | |
# A macro with two parameters implements the write system call | |
.macro write str, str_size | |
mov eax, 4 | |
mov ebx, 1 | |
lea ecx, \str | |
mov edx, \str_size | |
int 0x80 | |
.endm | |
# Implements the read system call | |
.macro read buff, buff_size | |
mov eax, 3 | |
mov ebx, 0 | |
lea ecx, \buff | |
mov edx, \buff_size | |
int 0x80 | |
.endm | |
.section .text | |
# Program entry point | |
.globl _start | |
_start: | |
write prompt_str, STR_SIZE | |
read buff, 32 | |
# Read returns the length in eax | |
push eax | |
# Print the hello text | |
write greet_str, GSTR_SIZE | |
pop edx | |
# edx = length returned by read | |
write buff, edx | |
_exit: | |
mov eax, 1 | |
mov ebx, 0 | |
int 0x80 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment