Created
May 10, 2014 22:56
-
-
Save shintakezou/04d6dda69a1dbeb523e5 to your computer and use it in GitHub Desktop.
Coroutines example in x86
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
extern printf | |
extern getchar | |
extern isalpha | |
section .data | |
templ: db "%s: %s",10,0 | |
align 2 | |
wordt: db "WORD",0 | |
align 2 | |
punct: db "PUNCT",0 | |
align 2 | |
list: dd wordt,punct | |
section .bss | |
buf: resb 256 | |
section .text | |
%define TPUNCT 1 | |
%define TWORD 0 | |
global main | |
main: | |
pusha | |
sub esp,8 | |
mov ebp,esp | |
mov dword [ebp+4],buf | |
lea eax,[getc] | |
mov [ebp],eax | |
call parse | |
add esp,8 | |
popa | |
ret | |
%macro JCONT 0 | |
mov ebx,[ebp] | |
mov dword [ebp],$+9 | |
jmp ebx | |
%endmacro | |
add_to_token: | |
pusha | |
mov edi,dword [ebp+4] | |
mov byte [edi],al | |
inc edi | |
mov [ebp+4],edi | |
popa | |
ret | |
got_token: | |
pusha | |
mov edi,dword [ebp+4] | |
mov byte [edi],0 | |
mov dword [ebp+4],buf | |
push buf | |
lea ebx,[list] | |
push dword [eax*4 + ebx] | |
push templ | |
call printf | |
add esp,12 | |
popa | |
ret | |
getc: | |
.loop: | |
call getchar | |
test eax,eax | |
js .end | |
cmp al,0xFF | |
jne .nocompr | |
call getchar | |
test eax,eax | |
js .end | |
push eax | |
call getchar | |
pop ecx | |
mov edx,eax | |
test eax,eax | |
js .end | |
.emitloop: | |
mov eax,edx | |
JCONT | |
loop .emitloop,cx | |
jmp .loop | |
.nocompr: | |
JCONT | |
jmp .loop | |
.end: | |
mov eax,-1 | |
JCONT | |
ret | |
parse: | |
JCONT | |
test eax,eax | |
js .end | |
push eax | |
push ecx | |
push eax | |
call isalpha | |
add esp,4 | |
pop ecx | |
pop edx | |
test eax,eax | |
jz .noalfa | |
.while: | |
mov eax,edx | |
call add_to_token | |
JCONT | |
test eax,eax | |
js .wend | |
push eax | |
push ecx | |
push eax | |
call isalpha | |
add esp,4 | |
pop ecx | |
pop edx | |
test eax,eax | |
jnz .while | |
.wend: | |
mov eax,TWORD | |
call got_token | |
test edx,edx | |
js .end | |
.noalfa: | |
mov eax,edx | |
call add_to_token | |
mov eax,TPUNCT | |
call got_token | |
jmp parse | |
.end: | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment