News:

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

About the printf() in the assembly language

Started by cjylg, November 21, 2008, 10:05:31 AM

Previous topic - Next topic

cjylg

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.)


jj2007

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

Vortex

Hi cjylg,

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

Result :

Index=5  Count=3  Element=680

BlackVortex

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.

cjylg

 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.

m7xtuf

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 !!!

MichaelW

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.
eschew obfuscation

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

m7xtuf

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 !!!

jj2007


m7xtuf

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

dedndave

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..."

m7xtuf

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 !!!

dedndave

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

MichaelW

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.
eschew obfuscation