News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Subroutine ignoredT_T

Started by falcon01, July 30, 2011, 09:39:39 AM

Previous topic - Next topic

falcon01

Hello, the code is very simple: I acquire 2 strings from keyboard and then jump to a routine called reorder:

include \masm32\include\masm32rt.inc

.stack  100h
.data

str1    db  "Insert first number: ",0
str2    db  "Insert second number: ",0

n1      db  100 dup(0)
n2      db  100 dup(0)

.code

start:  push    offset str1
        call    StdOut
        invoke   StdIn,addr n1,100
        push    offset str2
        call    StdOut
        invoke  StdIn,addr n2,100
        call    reorder          ;[b]this one is ignored!!![/b]

        push 0                   ;exiting
        call ExitProcess

reorder proc
        ...routine code...
reorder endp

           end  start



The program exits immediately without jumping to the subroutine...if I substitute "call" with "jmp" it works, so problem has to be in my soubroutine definition...

dedndave

we cannot see the routine code   :P

i noticed you use INVOKE and CALL interchangably
there are sometimes valid reasons for doing this
but i don't see a reason in the case of StdOut or ExitProcess
code is generally easier to follow using INVOKE
i, myself, use PUSH/CALL quite often, but with good reason

start:  INVOKE  StdOut,offset str1
        INVOKE  StdIn,offset n1,sizeof n1-1
        INVOKE  StdOut,offset str2
        INVOKE  StdIn,offset n2,sizeof n2-1
        call    reorder
        INVOKE  ExitProcess,0


as for the routine, make sure the stack is balanced and there is a RET at the end   :U

falcon01

Yes you're right, indeed I substituted calls with invokes.
Problem is that, with each subroutine I make, program just does not seem to enter/work!
Here's my last example: I tried to make a routine for the reading of a string, string is stored in a memory loc WITHOUT \n chars and acts this way:
1)Calls crt__getch in order to get a char in AL.
2)Checks if the char is 0dh (line feed, aka ENTER key pressed).
3)If different stores the char in memory, calls crt_putch in order to give user an echo and calls crt_getch again.
4)If equal exits.


include \masm32\include\masm32rt.inc

.stack  100h
.data

str1    db  "Insert first number: ",0
str2    db  "Insert second number: ",0

n1      db  100 dup(0)
n2      db  100 dup(0)

.code

start:  invoke  StdOut,offset str1
        lea     ebx,n1                          ;base address is stored in ebx
        call    getNum
       
fine:   invoke ExitProcess,0                ;exiting
     


;#############################################################################################

getNum      proc
                 push edi     
                 push eax

                 xor  edi,edi
                 xor  al,al

recN:       call crt__getch
       
            cmp  al,0dh
            je   endgetNum
            mov  BYTE PTR[ebx+edi],al ;insert in memory
            invoke crt__putch,al           ;echo
            inc  edi                             ;edi points to next byte
            jmp  recN

endgetNum:  pop eax
                   pop edi
                   ret
getNum      endp   
       
        end  start



program just freezes at call getNum instruction...

falcon01

By the way look at ollydbg's execution...shouldn't be an address in this blank zone?

falcon01

Ok solved:it was putch fault! I used getche in order to display echo, anyway now I got 2 important questions:
1)invoke StdOut overrides the current line...for example if I write "50" with my function and then I want to see the number and call
invoke StdOut,offset n1
I get, on console
Quote50sert first number:
...how come? :(

2)Ollydbg does not enter in a routine's body, that's why I though routine was ignored..how can I make it enter?

dedndave

your crt__putch call is over-writing the string because it starts at the "current cursor location"
you will have to set the cursor position
the problem here is, the masm32 StdOut routine uses WriteFile to output text
this method does not appear to be updating the location counters

i am sure the msvcrt has functions to do that, but i am not that familiar
you can use GetCursorPos and SetCursorPos API's

http://msdn.microsoft.com/en-us/library/ms648390%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms648394%28v=VS.85%29.aspx

another way to go would be to use the crt_puts function - or sprintf
there are several crt functions for outputing strings
they update the location counters
http://msdn.microsoft.com/en-us/library/tf52y4t1.aspx

once you get that up and running, i will remind you that you'll have to add code to handle backspace   :bg

Olly can certainly trace into routines
i hardly ever use a debugger   :P
one of the other members can tell you - or you can do like me and poke around the menus

Neil

As Dave says you are not updating the cursor position, an easy way to do this in console mode is to use the 'loc' macro.

Use it like this :- loc 28,9

This places the cursor position at line 10, column 29

remember that lines & columns start at zero.

with a standard console of 80 colums & 25 lines it's quite simple to work out where you want the cursor or on any other size of console.

dedndave

well - the problem goes farther than that
it seems to me that writing text to the console should update the location counters
if you look at the code for StdOut, you will see that it uses StrLen to get the length of the string
that information could be used to update the location counters independantly
i guess you'd have to look for carriage returns and line feeds - nothing is ever simple - lol
but, one approach might be to write a modified version of StdOut
it could use GetCursorPos prior to the write
then calculate the new position and use SetCursorPos after the write

dedndave

ok - looking a little deeper, it seems that StdOut does indeed update the counters
something else is amiss, here   :P

a while back, i wrote a function that had a default input string, as well as a prompt
i used the print macro to display the prompt, which uses StdOut i think

see the attachment...

falcon01

Discovered that problem is just in getch: putch updates cursor!
Oh and btw ollydbg really skips the subroutine unless you tell it to enter...lol! Ok this problem is solved too :D
2 dave:
I'll read your routine and try it, even if ... I don't like macrosXD I'm for an as more masm32.inc indipendent code as possible!

dedndave

the routine in the include file does not rely on the masm32 library at all
it uses only API functions and crt__kbhit, crt__getch

only the test/demo program uses the masm32 macros and library

oops - wrong thread   :P
i am refering to....
http://www.masm32.com/board/index.php?topic=17123.msg143069#msg143069

falcon01

Ok for 32 bit version, now I have to do a routine which takes a string on 16 bit one..I posted my question on this topic
http://www.masm32.com/board/index.php?topic=17146.msg143128#msg143128
could you please go and read it?The routine has just a little imperfection to me and should be easy to fix (to you obviouselyXD).