Skip to content

Instantly share code, notes, and snippets.

@arkenidar
Last active September 19, 2023 19:16
Show Gist options
  • Save arkenidar/91a7db3a69530a68fb9228b28824c54c to your computer and use it in GitHub Desktop.
Save arkenidar/91a7db3a69530a68fb9228b28824c54c to your computer and use it in GitHub Desktop.
SimpleAssembler aka SASM in Ubuntu and Windows
C:/apps/simple-asm/GAS/64/as.exe pclinwin.s -o program.o --64
C:/apps/simple-asm/MinGW64/bin/gcc.exe program.o -g -o program.exe -m64
## SASM mode: GAS x64
.file "pclinwin.s"
### gcc -no-pie -z noexecstack windows.s (Ubuntu)
### arkenidar@PC-HP-3:~/Dropbox/gh-repos/_drafts/asm$ gcc -no-pie -z noexecstack windows.s && ./a.out
# gcc -no-pie asm--program-01--GAS-X64-Assembly--SASM.s && ./a.* # example compilation (Ubuntu)
### -z noexecstack
# missing .note.GNU-stack section implies executable stack
###.section .note.GNU-stack,"",@progbits # added after above warning message (for Ubuntu, not Windows)
# un-comment in Linux , comment in Windows (... the line above)
# gcc -no-pie -z noexecstack pclinwin.s && ./a.out # in Ubuntu GNU+Linux
.data
count: .quad 0
# asciz means zero-terminated ASCII character-string
format_string: .asciz "Hello, world! [[%lld]]\n"
# no new-line (\n) for puts()
string01: .asciz "01: puts test"
string02: .asciz "02: puts test"
string_empty: .asciz ""
input_int_1: .quad 0
input_int_2: .quad 0
input_format_int_sum: .asciz "%lld%lld"
output_int_sum: .quad 0
output_format_int_sum: .asciz "output of integer sum: %lld \n \n"
.extern printf
.extern puts
.extern scanf
.text
.global main # program entry-point (beginning)
main:
movq %rsp, %rbp #for correct debugging
mov %rsp, %rbp # for correct debugging
sub $32, %rsp # only in SOME CASES (in Windows, not Ubuntu)
and $-16, %rsp # for correct debugging
# write your code here
# scanf : read 1 and 2
mov $input_format_int_sum, %rcx
mov $input_int_1, %rdx
mov $input_int_2, %r8
call scanf
# add : sum 1 and 2
movq input_int_1, %r13
addq input_int_2, %r13
mov %r13, output_int_sum
# printf : write sum
mov $output_format_int_sum, %rcx
mov output_int_sum, %rdx
call printf
# begin
movq $1, count # can be e.g. $3 (at least 1 as "do-while" construct)
loop_begin:
## https://en.wikipedia.org/wiki/X86_calling_conventions
# %rdi,%rsi,%rdx,%rcx,... (Ubuntu) or %rcx,%rdx,%r8,%r9,... (Windows)
# OS: windows (comment/un-comment)
#*
mov $format_string, %rcx
mov count, %rdx
call printf
mov $string01, %rcx
call puts
mov $string02, %rcx
call puts
mov $string_empty, %rcx
call puts
#*/
# OS: debian/ubuntu (comment/un-comment)
/*
mov $format_string, %rdi
mov count, %rsi
call printf
mov $string01, %rdi
call puts
mov $string02, %rdi
call puts
mov $string_empty, %rdi
call puts
#*/
# back-up
/*
mov $format_string, %rcx
mov count, %rdx
call printf
mov $string01, %rcx
call puts
mov $string02, %rcx
call puts
mov $string_empty, %rcx
call puts
#*/
# loop check
decq count
cmpq $0,count
jne loop_begin
# end
mov %rbp, %rsp # for correct debugging
mov $0, %rax # main returns 0
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment