Skip to content

Instantly share code, notes, and snippets.

@linuskmr
Last active May 4, 2021 13:50
Show Gist options
  • Save linuskmr/22ae0b4c70db6dcd8d3f2af49201d75e to your computer and use it in GitHub Desktop.
Save linuskmr/22ae0b4c70db6dcd8d3f2af49201d75e to your computer and use it in GitHub Desktop.
This program decrements the binary coded number on the tape and prints the contents of the tape (byte for byte)
# GNU Assembler, Intel syntax, x86_64 Linux
# This program decrements the binary coded number on the tape and prints the
# contents of the tape (byte for byte).
# Compile: clang -nostdlib -fno-integrated-as -Wa,-msyntax=intel,-mnaked-reg -s bin_decrement.s -o bin_decrement
# Run: ./bin_decrement | od -t d1
.data
.equ ASCII_BLANK, 95 # ASCII for '_'
.equ SYS_EXIT, 60 # Syscall number for exit
.equ EXIT_SUCCESS, 0
.equ SYS_WRITE, 1 # Syscall number for write
.equ STDOUT, 1 # FD for stdout
.equ TAPE_LEN, 7 # This must match the length of TAPE.
TAPE:
.byte ASCII_BLANK, 1, 1, 0, 0, 0, ASCII_BLANK
.text
.global _start
_start:
mov r12, offset TAPE # Write memory address of TAPE to r12
GOTO_LSB:
inc r12 # Move right
movb al, [r12] # Read current char
cmp al, ASCII_BLANK # Compare with blank
jne GOTO_LSB # Not yet on blank, so move on
SUBTRACT: # See insertion bin. subtraction part 2
dec r12 # Go to the left (at the beginning to LSB)
mov al, [r12] # Read current char
xor al, 1 # Bit-Flip (Only 0 and 1 on the tape here)
movb [r12], al # Write new char
test al, al # Char == 1?
jnz SUBTRACT # Execute SUBTRACT again
EXIT:
# Print tape data
# write(fd, buf, count)
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, offset TAPE # Address of tape
mov rdx, TAPE_LEN
syscall
# exit(code)
mov rax, SYS_EXIT
mov rdi, EXIT_SUCCESS
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment