Skip to content

Instantly share code, notes, and snippets.

@dekay
Last active August 26, 2023 16:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dekay/6049562 to your computer and use it in GitHub Desktop.
Save dekay/6049562 to your computer and use it in GitHub Desktop.
Listing 3 from http://www.ibm.com/developerworks/library/l-gas-nasm/ translated to GNU as and written using intel syntax.
# 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