flat assembler
Message board for the users of flat assembler.

Index > Windows > Double Looping

Author
Thread Post new topic Reply to topic
Wonfis



Joined: 23 Jul 2010
Posts: 1
Wonfis
I've just started getting to grips with assembly code in general and have been playing around with some commands.

I've got this loop that's supposed to simply count up to 5 from 0 while requiring a keyboard return between each number. It checks the return and if it sees "q" it exits. This works fine.

The problem is: every time I hit enter it performs two iterations of the loop. It did this with some earlier code I was playing around with too, which just used jumps instead of the loop command. The current stuff is below:

Code:
include 'win32ax.inc'

.data

inchar     DB   ?
numread    DD   ?
inhandle   DD   ?
outhandle  DD   ?
numwrit    DD   ?
numb       DB   "0"
quitmess   DB   "Enter 'q' to quit.", 10

.code

push    eax
push    ebx
push    ecx
push    edx

  start:

        invoke  AllocConsole
        invoke  GetStdHandle,STD_INPUT_HANDLE
        mov     [inhandle],eax
        invoke  GetStdHandle,STD_OUTPUT_HANDLE
        mov     [outhandle],eax
        xor     eax,eax
        mov     ecx,5

  lab:
        push    ecx
        invoke  WriteConsole,[outhandle],numb,1,numwrit,0
        invoke  ReadConsole,[inhandle],inchar,1,numread,0
        pop     ecx

        mov     bl,[inchar]
        cmp     bl,"q"
        jz      exitl
        inc     byte    [numb]

        loop    lab



  exitl:

  pop   edx
  pop   ecx
  pop   ebx
  pop   eax

        invoke ExitProcess,0

.end start                      


So why is it doing this? I'm running on XP Pro SP3.
Post 23 Jul 2010, 11:30
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17473
Location: In your JS exploiting you and your system
revolution
ReadConsole returns more than one keystroke for some keys. Try displaying what ReadConsole returns to you and you will see. Alternatively you can try http://ollydbg.de to watch what happens.
Post 23 Jul 2010, 11:54
View user's profile Send private message Visit poster's website Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
You can trick it by reading 2 bytes from the stream.
Its because ReadConsole blocks (w/echo) until VK_RETURN is found.
Code:
format PE GUI 4.0
entry start

include 'win32a.inc'

section '.code' code readable executable

  start:
        invoke  AllocConsole
        invoke  GetStdHandle,STD_INPUT_HANDLE
        mov     [g_stdin],eax
        invoke  GetStdHandle,STD_OUTPUT_HANDLE
        mov     [g_stdout],eax
    .mloop:
        invoke  WriteConsole,[g_stdout],g_number,1,g_iobytes,0
        invoke  ReadConsole,[g_stdin],g_inchar,2,g_iobytes,0

        cmp     byte[g_inchar],'q'
        je      .quit
        inc     [g_number]
        cmp     [g_number],'5'
        jl      .mloop

    .quit:
        invoke ExitProcess,0

section '.data' data readable writeable

  g_stdin   dd ?
  g_stdout  dd ?
  g_iobytes dd ?
  g_inchar  dw ?
  g_number  db '0'

section '.idata' import data readable

  library kernel32,'kernel32.dll',\
          user32,'user32.dll'

  include 'api\kernel32.inc'
  include 'api\user32.inc'
    
Post 23 Jul 2010, 23:22
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 YouTube, Twitter.

Website powered by rwasa.