Skip to content

Instantly share code, notes, and snippets.

@erikroyall
Created October 24, 2016 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikroyall/dc7e3f3dbb94eb1f031e359d5e000ee4 to your computer and use it in GitHub Desktop.
Save erikroyall/dc7e3f3dbb94eb1f031e359d5e000ee4 to your computer and use it in GitHub Desktop.
~/learn/asm » cat ~/bin/asm erik@royall
#!/bin/bash
# Simple assemble/link script
if [ -z $1 ]; then
echo "Usage: ./asm <asmMainFile> (no extension)"
exit
fi
# Verify no extensions were entered
if [ ! -r "$1.asm" ]; then
echo "Error, $1.asm not found"
echo "Note, do not exter file extensions."
exit
fi
# Compile, assemble and link
yasm -g dwarf2 -f elf64 $1.asm -l $1.lst
ld -g -o $1 $1.o
------------------------------------------------------------
~/learn/asm » cat ex.asm erik@royall
; Simple example demonstrating basic program
; format and layout.
; ******************************************
; Some basic data declarations
section .data
; -----
; Define Constants
EXIT_SUCCESS equ 0 ; Successful operation
SYS_exit equ 60 ; call code for terminate
; -----
; Byte (8-bit) variable declarations
bVar1 db 17
bVar2 db 9
bResult db 0
; -----
; Word (16-bit) variable declarations
wVar1 dw 17000
wVar2 dw 9000
wResult dw 0
; -----
; Double-word (32-bit) variable declarations
dVar1 dd 17000000
dVar2 dd 9000000
dResult dd 0
; -----
; Quad-word (64-bit variable declarations)
qVar1 dq 170000000
qVar2 dq 90000000
qResult dq 0
; ******************************************
; Code Section
section.text
global _start
_start:
; Performs a series of very basic addition operations
; to demonstrate basic program format.
; -----
; Byte example
; bResult = bVar1 + bVar2
mov al, byte [bVar1]
add al, byte [bVar2]
mov byte [bResult], al
; Word
mov ax, word [wVar1]
add ax, word [wVar2]
mov word [wResult], ax
; Double-word
mov eax, dword [dVar1]
add eax, dword [dVar2]
mov dword [dResult], eax
; Quad-word
mov rax, qword [qVar1]
add rax, qword [qVar2]
mov qword [qResult], rax
last:
mov rax, SYS_exit ; Call code for exit
mov rdi, EXIT_SUCCESS ; Exit program with success
syscall
------------------------------------------------------------
~/learn/asm » ~/bin/asm ex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment