amd64-postfix/postfix.s

88 lines
1.4 KiB
ArmAsm

.text
.global _start
_start:
mov $buf, %rsi
readloop:
## Read character in.
mov $0, %rax
mov $0, %rdi
mov $1, %rdx
syscall
## Check return value; exit if not equal to one.
cmp $1, %rax
jne exit
## Move read character into RAX and increment buffer position.
movzx (%rsi), %rax
inc %rsi
## Loop if read character wasn't a line feed.
cmp $0x0a, %rax
jne readloop
## Start reading words at start of buffer with zero length
mov $buf, %rsi
mov $0, %rdx
wordloop:
## Copy character into R8
movzx (%rsi, %rdx), %r8
## Print the word if the character is a space or a newline
cmp $0x20, %r8
je doword
cmp $0x0a, %r8
je doword
## Increment word length and loop
inc %rdx
jmp wordloop
doword:
mov $0, %rax
mov $0, %rcx
numloop:
## Multiply current value by ten
imul $10, %rax
## Add numeric value of digit
movzx (%rsi, %rcx), %rbx
sub $0x30, %rbx
add %rbx, %rax
## Increment index and loop if not at end of word
inc %rcx
cmp %rcx, %rdx
jg numloop
push %rax
endword:
## Go back to start if the terminating character (which is in R8)
## of the word was a line feed
cmp $0x0a, %r8
je _start
## Add the word length to RSI and increment until non-space is
## found
add %rdx, %rsi
spaceloop:
inc %rsi
mov (%rsi), %rax
cmp $0x20, %rax
je spaceloop
## Zero the word length then return to the word loop.
mov $0, %rdx
jmp wordloop
exit:
## Exit with code 0
mov $60, %rax
mov $0, %rdi
syscall
.bss
buf:
.skip 100