I try to implement the python/c# yield operator in asm, but failed. Yield is multiple returning a value. [EDIT: Almost succeded]
format pe console 5.0
entry start
include 'C:\Program Files\FASM\include\win32a.inc'
fmt db '%i',10,13,0
buff rb 128
; print ints()
;
; sub ints()
; for i = 1 to 5
; yield i
; next i
; end sub
macro yield val {
mov eax, val ; eax = current yield return value
pushad ; save all registers
push @f ; [esp-4] = pointer to next opcode [1]
push @b ; [esp-0] = pointer to next yield operation [2]
ret
@@: ; [1]
popad ; load all registers
}
start:
call ints
@@: ; [2]
invoke wsprintf,buff,fmt,eax
add esp, 4*3
invoke WriteFile,7,buff,eax,0,0
ret ; continue the code in yield, jumps to [esp-0] [1]
invoke ExitProcess,0
;proc intmul
; push ebp
; mov ebp, esp
;
; call ints
; imul eax, 5
; yield eax
;
; mov esp, ebp
; pop ebp
; ret
;endp
proc ints
push ebp
mov ebp, esp
mov ecx, 1
@next:
yield ecx
add ecx, 1
cmp ecx, 5
jng @next
invoke ExitProcess,0
mov esp, ebp
pop ebp
ret
endp
section '.idata' import data readable
library kernel32,'KERNEL32.DLL',\
user32, 'USER32.DLL'
include 'c:\program files\fasm\include/api/kernel32.inc'
include 'c:\program files\fasm\include/api/user32.inc'