Created
February 3, 2017 23:11
-
-
Save tbodt/b33a5de04ef27f05a8c5488f5824afac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# What does the fox say? This program will tell you, but only if you tell it. | |
# Proud author: Theodore Dubois | |
.data | |
dataStart: | |
animalPrompt: | |
.int 16 | |
.ascii "Type an animal: " | |
soundPrompt: | |
.int 18 | |
.ascii "What does it say? " | |
response1: | |
.int 4 | |
.ascii "The " | |
response2: | |
.int 6 | |
.ascii " says " | |
response3: | |
.int 2 | |
.ascii "!\n" | |
animalBuffer: | |
.space 4 # the length | |
.space 128 # I love powers of 2. | |
soundBuffer: | |
.space 4 | |
.space 128 | |
.text | |
.global _start | |
_start: | |
# I've written a lot of subroutines. | |
# The next few lines should be really obvious. | |
mov $animalPrompt, %eax | |
call write | |
mov $animalBuffer, %eax | |
call read | |
mov $soundPrompt, %eax | |
call write | |
mov $soundBuffer, %eax | |
call read | |
mov $response1, %eax | |
call write | |
mov $animalBuffer, %eax | |
call write | |
mov $response2, %eax | |
call write | |
mov $soundBuffer, %eax | |
call write | |
mov $response3, %eax | |
call write | |
# exit | |
mov $1, %eax # exit syscall number | |
mov $0, %ebx # exit status | |
int $0x80 | |
# Now, the subroutines. It's normal to be scared. | |
# Print something, quick and easy. | |
# Put the output buffer in %eax before calling. | |
write: | |
mov (%eax), %edx # count | |
add $4, %eax # skip over size | |
mov %eax, %ecx # address | |
mov $4, %eax # write syscall number | |
mov $1, %ebx # stdout | |
int $0x80 | |
ret | |
# Read into the buffer, quick and easy. | |
# Put the buffer in %eax before calling. | |
# The first 4 bytes of the buffer are the length, then the actual stuff. | |
# The newline is removed, at no extra charge. | |
read: | |
# The buffer goes in %eax for convenience, but things clobber it, so I'm using %edi | |
mov %eax, %edi | |
add $4, %edi # skip the size | |
mov $3, %eax # read syscall number | |
mov $0, %ebx # stdin | |
mov %edi, %ecx # the buffer | |
movl $128, -4(%edi) # now we have to subtract 4 to get the size | |
int $0x80 | |
dec %eax # take off the newline | |
mov %eax, -4(%edi) # put the size in the right place | |
ret | |
# Tell vim that this is a GNU assembler file | |
/* vim: ft=gas : | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment