News:

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

need help in printing integers

Started by peedarpk, February 17, 2010, 07:31:10 AM

Previous topic - Next topic

peedarpk

Hi all, this is my first post in this forum. i am new to assembly, just started four days back.
What i want to do is this, Print integers 1 - 10 on to the screen

this is the code i have written so far but it doesn't seem to work.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main
    inkey
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc
    mov ax, 1
    mov bx, 10

begin_loop: 
    print str$(eax)   
    inc ax
    cmp ax, bx
    jle begin_loop

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start

please help, i think the way i am trying to print the integers is wrong

BlackVortex

I think the "print" macro modifies some registers, not sure. Anwyay, check this thread to see how to convert a register's value to a string :
http://www.masm32.com/board/index.php?topic=13392.0

Neil

The print macro destroys the contents of the eax register, you need to push it onto the stack & pop it after.

Neil

I've had a bit more time to study your code & another problem is that you are not using the full 32 bit eax register, so the hi word of it could contain anything & if bit 31 is set you will get a negative number. str$ is for signed numbers, for unsigned use ustr$.

peedarpk

Thanks a lot BlackVortex  & Neil .

Quote from: Neil on February 17, 2010, 08:06:39 AM
The print macro destroys the contents of the eax register, you need to push it onto the stack & pop it after.

I tried what you have said and and my code seems to be working now. (Although i don't fully understand how its happening ) , here is the working codes please guide me

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main
    inkey
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc
   
    mov ebx, 10
    mov eax, 1
begin_loop:

    mov ecx,0
    add ecx,eax
    push eax
    push ecx
    print ustr$(ecx)
    print chr$(',')
    pop ecx
    pop eax
    add eax,1
    cmp eax, ebx
    jle begin_loop

    print chr$(13,10,13,10)
    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start

dedndave

the print macro destroys EDI, as well i think - a bug that should be fixed on the next release

        mov     eax,1

digits: push    eax
        print   ustr$(eax),','
        pop     eax
        inc     eax
        cmp     eax,10
        jb      digits

        print   ustr$(eax),13,10

Neil

peedarpk your code works OK but it's far too complicated, study dave's code which does exactly what you want.
One other point when you want to zero a register use the instruction sub reg,reg or as some prefer xor reg,reg.

jj2007

Quote from: dedndave on February 17, 2010, 10:08:58 AM
the print macro destroys EDI, as well i think - a bug that should be fixed on the next release

Test yourself :8)

include \masm32\include\masm32rt.inc

.code
AppName db "Masm32:", 0

start: mov edi, 123
print "Hello World"
.if edi!=123
print " - and bye bye edi..."
.endif
inkey chr$(13, 10, 10, "hit any key to get outta here")
exit

end start

peedarpk

Thanks a lot dedndave ,jj2007 .

jj2007 tried the code  :thumbu

jj2007

Practically all Masm32 library macros, with the exception of certain high speed copy algos, preserve edi esi ebx. So does print (Dave, you should update your version). Here are some hints.

Quote- When calling Windows APIs, e.g. invoke MessageBox, 0, str$(eax), chr$("Title"), MB_OK,
  the registers esi edi ebx ebp esp will be preserved by Windows; eax will return a value, and
  ecx and edx will most probably contain garbage. Beginners stumble sooner or later over this problem
  when they use ecx for a loop and see a crash.

- Another popular error: Masm uses by default unsigned comparisons. This example demonstrates the effect:
   mov eax, -1   ; definitely a lot smaller than 99, right?
   .if eax<99
      MsgBox 0, str$(eax), "Eax is less than 99:", MB_OK
   .else
      MsgBox 0, str$(eax), "Surprise, surprise: eax is bigger than 99!", MB_OK
   .endif

dedndave

i seem to recall having to preserve ebx, esi, or edi across print at one time
i don't remember what the case was, now
but, i tested it and they all seem to be preserved - brain fart, i guess
maybe i had an old copy and since that time, updated it
maybe it was one of the chr$, uhex$, or ustr$ macros

Slugsnack

although the macros are not function calls, they tend to roughly follow stdcall convention

jj2007

MichaelW:
QuoteOnly about half of the macros that use ESI and/or EDI preserve them. ShellAboutBox and Mcopy alter both, lob alters ESI, and stb alters EDI. For Mcopy, lob, and stb, I think the registers are not preserved as a speed optimization.

In practice, I have never had problems with any Masm32 macro. You can assume that esi edi ebx are safe.