Skip to content

Instantly share code, notes, and snippets.

@dekay
Created July 21, 2013 19:03
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/6049575 to your computer and use it in GitHub Desktop.
Save dekay/6049575 to your computer and use it in GitHub Desktop.
Listing 5 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 nasm2gas5.o nasm2gas5.s
# Link with: ld --dynamic-linker /lib/ld-linux.so.2 -lc -o nasm2gas5 nasm2gas5.o
#
# Support intel syntal vs. ATT and don't use % before register names
.intel_syntax noprefix
.section .data
# Command table to store at most 10 command line arguments
cmd_tbl: .rept 10
.long 0
.endr
.section .text
# Program entry point
.globl _start
_start:
# Set up the stack frame
mov ebp, esp
# Top of stack contains the number of command line arguments. Default is 1.
mov ecx, [ebp]
# Exit if arguments are more than 10
cmp ecx, 10
jg _exit
mov esi, 1
mov edi, 0
# Store the command line arguments in the command table
store_loop:
mov eax, [ebp + esi * 4]
mov [OFFSET FLAT:cmd_tbl + edi * 4], eax
inc esi
inc edi
loop store_loop
mov ecx, edi
mov esi, 0
print_loop:
#Make some local space
sub esp, 4
# puts function corrupts ecx
mov [ebp - 4], ecx
mov eax, [OFFSET FLAT:cmd_tbl + esi * 4]
push eax
call puts
add esp, 4
mov ecx, [ebp - 4]
inc esi
loop print_loop
jmp _exit
_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