The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: cjylg on November 21, 2008, 10:05:31 AM

Title: About the printf() in the assembly language
Post by: cjylg on November 21, 2008, 10:05:31 AM
I had wrote some codes of assembly language.Those codes are not wrong,but when I chicked the Run ProGram of the Project in the menu bar of masm32,nothing happened.I think the question iscause by printf(). I check the codes whole two days,but found nothing.The version of masm32 which I use is masm32 v10.
one code is underside:


.386
.model flat,stdcall
option casemap:none
include \masm32\include\msvcrt.inc
includelib  \masm32\lib\msvcrt.lib

.data
dArray      dword  50,78,99,200,451,680,718,820,1000,2000
ITEMS       equ    ($-dArray)/4
Element     dword   680
Index       dword   ?
Count       dword   ?
szFmt       db      'Index=%d  Count=%d  Element=%d',0
szErrMsg    db      'Not found,Count=%d  Element=%d',0
.code
start:
            mov     Index,-1
            mov     Count,0
            mov     ecx,0
            mov     edx,ITEMS-1
            mov     eax,Element
b10:
            cmp     ecx,edx
            jg      b40
            mov     esi,ecx
            add     esi,edx
            shr     esi,1
            inc     Count
            cmp     eax,dArray[esi*4]
            jz      b30
            jg      b20
            mov     edx,esi
            dec     edx
            jmp     b10
b20:
            mov     ecx,esi
            inc     ecx
            jmp     b10
b30:
            mov     Index,esi
            invoke  crt_printf,addr szFmt,Index,Count,dArray[esi*4]
            jmp     b50
b40:
            invoke  crt_printf,addr  szErrMsg,Count,Element
b50:
            ret
end         start

Who can help me?And tell me something about how to use printf() and how to use library of C language in the assembly language.Thank you ::)

(I'm a biginner and my english is so-so.)

Title: Re: About the printf() in the assembly language
Post by: jj2007 on November 21, 2008, 02:36:00 PM
Assemble as CONSOLE application, and run it from a commandline prompt.
It assembles fine indeed, and I get this output:

Index=5  Count=3  Element=680
Title: Re: About the printf() in the assembly language
Post by: Vortex on November 21, 2008, 06:36:36 PM
Hi cjylg,

Your code works fine. As jj2007 explained, run your application from the command prompt.

Result :

Index=5  Count=3  Element=680
Title: Re: About the printf() in the assembly language
Post by: BlackVortex on November 21, 2008, 06:50:14 PM
Quote from: cjylg on November 21, 2008, 10:05:31 AM
And tell me something about how to use printf() and how to use library of C language in the assembly language.Thank you ::)

Hey, masm32 has its own awesome library and macros, too. Check the docs in the \masm32\help folder.
Title: Re: About the printf() in the assembly language
Post by: cjylg on November 22, 2008, 10:16:26 AM
 Thanks jj2007,Vortex and BlackVortex to help me   :bg
I had to do as you say,and run it in the command prompt,I got the right output.
And as BlackVortex says,masm32 has its own awesome library and macros,the help file of masm32 is useful.
Title: Re: About the printf() in the assembly language
Post by: m7xtuf on November 13, 2009, 01:44:42 AM
Hi there,

  I am new in MASM as well.  I have tried the code and the code will run.  However, I see nothing (or don't know where to check the result).  I guess because it is a printf program and the program exit right away.  Is there a way to see "the result" ???  I have also try to run it in a command prompt.  It just run and nothing display as well.  Thanx for help !!!
Title: Re: About the printf() in the assembly language
Post by: MichaelW on November 13, 2009, 03:49:26 AM
How did you assemble and link? If you linked it as a GUI app instead of a console app, then it would run and exit without displaying anything. If you are using the MASM32 Quick Editor, you should select Console Assemble & Link on the Project menu.
Title: Re: About the printf() in the assembly language
Post by: hutch-- on November 13, 2009, 04:19:19 AM
Hi m7xtuf,

Welcome on board.

If you build the app as console as Michael suggested and you run it without a method of holding the app open, it can also go that fast that you don't see it byut there is a macro in the masm32 project designed to solve that problem, "inkey".

Just put "inkey" where you want to hold up the app and it will stop there waiting for a keystroke.
Title: Re: About the printf() in the assembly language
Post by: m7xtuf on November 13, 2009, 09:53:25 PM
Hi MichaleW and hutch,

   Like MichaelW mentioned, I used MASM32 Quick Editor, and try "Console Assemble & Link" on the Project menu.  Then I "Run Program" from the Project menu.  Still, never happen (or it run and quick right away).

   With hutch, where should I put the "inkey" but I got a syntax error when trying to assemble.  Can you show me with the "given example" where to put the "inkey" or what library I should include to avoid the assemble error.

   Many many thanx !!!
Title: Re: About the printf() in the assembly language
Post by: jj2007 on November 13, 2009, 11:14:33 PM
Use inkey as follows:

inkey "Press any key"
Title: Re: About the printf() in the assembly language
Post by: m7xtuf on November 14, 2009, 12:37:14 AM
I still get the syntax error when compiling with "inkey" ... this is what I do from the sample code ... HELP is still needed !!!

<deleted>

b20:
            mov     ecx,esi
            inc     ecx
            jmp     b10
b30:
            mov     Index,esi
            invoke  crt_printf,addr szFmt,Index,Count,dArray[esi*4]
            jmp     b50
b40:
            invoke  crt_printf,addr  szErrMsg,Count,Element
b50:
            inkey "press any key"
            ret
end       start
Title: Re: About the printf() in the assembly language
Post by: dedndave on November 14, 2009, 12:58:18 AM
welcome to the forum   :U

as i recall, the inkey macro does not like double quotes - try single quotes
but, if no text is present at all, it displays "Press any key to continue..."
Title: Re: About the printf() in the assembly language
Post by: m7xtuf on November 14, 2009, 01:47:51 AM
I must be missing some library or what ... everyone told me that the "inkey" should work, but I got assemble syntax error ... looks like "inkey" is not defined ...  HELP is needed !!!
Title: Re: About the printf() in the assembly language
Post by: dedndave on November 14, 2009, 02:55:27 AM
ok - we all use a different include - lol
remove all this stuff:

.386
.model flat,stdcall
option casemap:none
include \masm32\include\msvcrt.inc
includelib  \masm32\lib\msvcrt.lib

if you use:

        include \masm32\include\masm32rt.inc

it adds most of the stuff you will ever need
you should take a peek inside that file to see what it has in it
it will save you some typing to just use masm32rt.inc


EDIT
the only things i have ever had to add were:

        include    \masm32\include\advapi32.inc
        includelib \masm32\lib\advapi32.lib

when i want to use the API registry functions
oh - and sometimes, .586 or .686/.xmm/.mmx
Title: Re: About the printf() in the assembly language
Post by: MichaelW on November 14, 2009, 05:15:21 AM
To use the inkey macro, as a minimum you would need to add the following to your source file:

include \masm32\macros\macros.asm
include \masm32\include\masm32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\kernel32.lib


So it would be easier to just do as Dave suggested.

Or with the current source file you can add a call to the CRT getch function:

invoke crt__getch

And this will cause the app to wait for a keystroke before it exits.
Title: Re: About the printf() in the assembly language
Post by: dsouza123 on November 14, 2009, 04:51:32 PM
An alternative that allows extra options, instead of   

inkey "press any key"

use

inkey  addr szPressKey

and in the .data section

szPressKey  db  13,10,"Press any key",0

the 13,10,  is for cr lf (carriage return  linefeed)
so the message will show up on a separate line.
the ,0 to make sure the string is zero terminated.

In general adding ,13,10 at the end of a string
or embedded in it as in  "text",13,10,"more text",13,10 eventually ending the string with ,0
enables placing the text on different lines.
Title: Re: About the printf() in the assembly language
Post by: m7xtuf on November 16, 2009, 06:23:22 PM
Thank you all  !!!

The "inkey" is working fine !!!

Thank you !!!
Title: Re: About the printf() in the assembly language
Post by: m7xtuf on November 17, 2009, 07:37:19 PM
Hi guys,

   I have another question ... I am using the MASM32 editor.  Is there any debugger available so that I can debug/trace the code.  Also it will be nice if there is a memory view and register view ... any recommendation ???

   Thanx once again !!!
Title: Re: About the printf() in the assembly language
Post by: dedndave on November 17, 2009, 09:18:03 PM
Olly...
http://www.ollydbg.de/
Title: Re: About the printf() in the assembly language
Post by: m7xtuf on November 19, 2009, 12:34:01 AM
Thanx ... and any other recommendation for another debugger ???
Title: Re: About the printf() in the assembly language
Post by: dedndave on November 19, 2009, 12:40:04 AM
well - visual studio has one, i think
other than that, i believe most guys in here use Olly's
i am kind of a newbie to 32-bit, so i didn't question their choice - lol
thing about Olly is, it allows plug-ins that let you extend its' capability
Title: Re: About the printf() in the assembly language
Post by: jj2007 on November 19, 2009, 12:59:23 AM
Olly is clearly the best (although some masochists here seem to use WinDbg, too ::))

I have configured my own IDE (RichMasm, comes with MasmBasic) so that if code contains an active (i.e. non-commented) int 3, the IDE launches Olly directly after successful assembly. And I use that feature an awful lot - Olly is essential for understanding low level code.

Olly's only weak point is symbols. Certain older versions work with simple code, but I have not been able to convince the newer 2.0 version to use my own variable and proc names. Unfortunately the developer (Oleh Yuschuk) seems to have lost interest, or has too much other work, who knows - the last version (http://www.ollydbg.de/version2.html) dates March 23, 2009.
Title: Re: About the printf() in the assembly language
Post by: japheth on November 19, 2009, 03:47:28 AM

True assembly coders use CDB or NTSD.
Title: Re: About the printf() in the assembly language
Post by: BogdanOntanu on November 19, 2009, 07:54:37 AM
Quote from: jj2007 on November 19, 2009, 12:59:23 AM
...
Olly's only weak point is symbols. Certain older versions work with simple code, but I have not been able to convince the newer 2.0 version to use my own variable and proc names. Unfortunately the developer (Oleh Yuschuk) seems to have lost interest, or has too much other work, who knows - the last version (http://www.ollydbg.de/version2.html) dates March 23, 2009.

That is because you insist to use Ollydbg version 2.0 and this version is work in progress... not finished and with no support for symbols... yet.

Alternatively if you will use the "old" but fully functional Ollydbg version 1.10 then it will show you variables and procedure names (build your exe with debug info)
Title: Re: About the printf() in the assembly language
Post by: jj2007 on November 19, 2009, 08:15:33 AM
Quote from: BogdanOntanu on November 19, 2009, 07:54:37 AM
Alternatively if you will use the "old" but fully functional Ollydbg version 1.10 then it will show you variables and procedure names (build your exe with debug info)

Bogdan, is there a way to convince Olly 1.10 that XMM registers should be shown as integers?
Title: Re: About the printf() in the assembly language
Post by: BogdanOntanu on November 19, 2009, 10:09:09 AM
Quote from: jj2007 on November 19, 2009, 08:15:33 AM
...
Bogdan, is there a way to convince Olly 1.10 that XMM registers should be shown as integers?

NO. AFAIK normal Olly 1.10 will not even show XMM registers or SSE2+ instructions.... but there might be an plugin or a patched Olly 1.0 available :D

However for debugging and testing "normal" code  OllyDbg v1.10 is a good tool.
Title: Re: About the printf() in the assembly language
Post by: jj2007 on November 19, 2009, 11:21:51 AM
Quote from: japheth on November 19, 2009, 03:47:28 AM

True assembly coders use CDB or NTSD.

Yep, it's a jewel :bg

Microsoft (R) Windows User-Mode Debugger  Version 5.1.2600.0
Copyright (c) Microsoft Corporation. All rights reserved.

CommandLine: bin2byte.exe
Loaded dbghelp extension DLL
The call to LoadLibrary(ext) failed with error 2.
Please check your debugger configuration and/or network access
Loaded exts extension DLL
The call to LoadLibrary(uext) failed with error 2.
Please check your debugger configuration and/or network access
Loaded ntsdexts extension DLL
Symbol search path is: *** Invalid *** : Verify _NT_SYMBOL_PATH setting
Executable search path is:
ModLoad: 00400000 00406000   image00400000
ModLoad: 7c900000 7c9b0000   ntdll.dll
ModLoad: 7c800000 7c8f4000   C:\WINDOWS\system32\kernel32.dll
ModLoad: 77c10000 77c68000   C:\WINDOWS\system32\msvcrt.dll
Break instruction exception - code 80000003 (first chance)
eax=00241eb4 ebx=7ffd5000 ecx=00000004 edx=00000010 esi=00241f48 edi=00241eb4
eip=7c901230 esp=0012fb20 ebp=0012fc94 iopl=0         nv up ei pl nz na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000202
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for ntdl
l.dll -
ntdll!DbgBreakPoint:
7c901230 cc               int     3
0:000>
Title: Re: About the printf() in the assembly language
Post by: GregL on November 21, 2009, 02:04:13 AM
Quote from: jjOlly is clearly the best (although some masochists here seem to use WinDbg, too Roll Eyes)

OllyDbg is good, but Visual Studio is the best, it does 64-bit too. VC++ Express Edition or the VS 2010 beta work great.

I never cared for WinDbg either.

CDB or NTSD ... Ugh!
Title: Re: About the printf() in the assembly language
Post by: japheth on November 21, 2009, 11:41:05 AM
Quote from: jj2007 on November 19, 2009, 11:21:51 AM
Microsoft (R) Windows User-Mode Debugger  Version 5.1.2600.0
Copyright (c) Microsoft Corporation. All rights reserved.

CommandLine: bin2byte.exe
Loaded dbghelp extension DLL
The call to LoadLibrary(ext) failed with error 2.
Please check your debugger configuration and/or network access
Loaded exts extension DLL
The call to LoadLibrary(uext) failed with error 2.
Please check your debugger configuration and/or network access
Loaded ntsdexts extension DLL

Perhaps you should consider to download a more recent version of the debugging tools for Windows. AFAICS your dbgeng.dll is the original XP one, dated 2001. This will also remove the error messages which you're experiencing.

Quote
Symbol search path is: *** Invalid *** : Verify _NT_SYMBOL_PATH setting
Executable search path is:
ModLoad: 00400000 00406000   image00400000
ModLoad: 7c900000 7c9b0000   ntdll.dll
ModLoad: 7c800000 7c8f4000   C:\WINDOWS\system32\kernel32.dll
ModLoad: 77c10000 77c68000   C:\WINDOWS\system32\msvcrt.dll
Break instruction exception - code 80000003 (first chance)
eax=00241eb4 ebx=7ffd5000 ecx=00000004 edx=00000010 esi=00241f48 edi=00241eb4
eip=7c901230 esp=0012fb20 ebp=0012fc94 iopl=0         nv up ei pl nz na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000202
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for ntdl
l.dll -
ntdll!DbgBreakPoint:
7c901230 cc               int     3
0:000>

Ok. you're now - almost - at the very beginning of the debugging process. With a "g main" or "g start" or whatever your entry is you'll get to your program. IMO a pretty good and solid design. Btw, CDB has no problems displaying XMM registers in any format you want.  :8)