flat assembler
Message board for the users of flat assembler.

Index > Windows > delphi to asm

Thread Post new topic Reply to topic

Joined: 03 Aug 2009
Posts: 1
hi guys! help me please translate this delphi code into asm.
procedure pravo;
  keybd_event(vk_right, 0, 0, 0);
  keybd_event(vk_right, 0, KEYEVENTF_KEYUP, 0);

procedure levo;
  keybd_event(vk_left, 0, 0, 0);
  keybd_event(vk_left, 0, KEYEVENTF_KEYUP, 0);

  procedure up;
  keybd_event(vk_up, 0, 0, 0);
  keybd_event(vk_up, 0, KEYEVENTF_KEYUP, 0);

  procedure down;
  keybd_event(vk_down, 0, 0, 0);
  keybd_event(vk_down, 0, KEYEVENTF_KEYUP, 0);

procedure TForm1.Timer1Timer(Sender: TObject);
  while g < 90 do
if a=0 then pravo;
if a=1 then down;
if a=2 then levo;
if a=3 then up;


P.S. Sorry for my english
Post 03 Aug 2009, 17:59
View user's profile Send private message ICQ Number Reply with quote

Joined: 27 Dec 2004
Posts: 805
Your UP, DOWN, LEFT, RIGHT procs will look almost the same in ASM, since they just call a windows api from user32.dll
  invoke keybd_event,VK_DOWN, 0, 0, 0
  invoke keybd_event,VK_DOWN, 0, KEYEVENTF_KEYUP, 0
  ret 0

Your timer event handler can be ported a number of ways to ASM using the CreateThread api or actually creating a timer using the SetTimer api.
Since you didn't provide any timer creation code I assume delphi does this for you (I recall it being similar to VB with a drag-N-drop IDE).

For simplicity here is the time creation routine using CreateThread
timerDelayInMS dd 2000; every 2 seconds
timerThreadID dd 0; this will holder the timer thread ID
timerThreadHandle dd 0; the thread handle
  invoke CreateThread, 0, 0, TimerProc, 0, 0, timerThreadID
  mov dword[timerThreadHandle],eax
  ret 0

And finally there's your Time1Timer proc because I'm simulating the timer with a thread there will be an outer loop representing the intervals.
TimerProc: ;referenced in the above code snippet for CreateThread
push ebx esi edi

.TimerLoop: ;infinitely loop with a delay
invoke SleepEx, dword[timerDelayInMS],0 ;timerDelay referenced in above snippet
;SleepEx with suspend the thread for timerDelayInMS milliseconds

mov ebx,0 ; g=0
.GLoop: ;while g<90

;random(4) we can simulate this in ASM using RDTSC
;this will retrieve the processors internal tick counter
;while it is NOT random it is pseudo random enough for our use
rdtsc ; a = rand(4); a will be our esi register
mov esi,eax
and esi,3 ;clamp result between 0 and 3 
;this is a shortcut because 4 is a power of two 
;so 4-1 can be used as a clamping mask
cmp esi,0 ; if a = 0
jne .skip0
call pravo
cmp esi,1 ; if a = 1
jne .skip1
call down
cmp esi,2 ; if a = 2
jne .skip2
call levo
cmp esi,3 ; if a = 3
jne .skip3
call up

add ebx,1 ;g = g + 1
cmp ebx,90
jl .GLoop ; while g < 90 continue looping

jmp .TimerLoop

pop edi esi ebx
ret 4

The above was written in my browser window (it is NOT tested) so there may be errors. If you make an attempt to write the ASM yourself using the examples folder as a reference we can help you from there.

*edited * good catch * to preserve "a" using the ESI register instead of EAX

Last edited by r22 on 03 Aug 2009, 19:40; edited 1 time in total
Post 03 Aug 2009, 19:15
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote

Joined: 30 Jun 2004
Posts: 827
Location: Michigan, USA
Your call codes could possibly modify eax, and possibly make more than one of the button calls be taken. I know it's just an example to show how its basically done, but you don't want to start his assembly learning off with accidental bad code.

----> * <---- My star, won HERE
Post 03 Aug 2009, 19:35
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum

Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.