News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How to print a multiply table in MASM32

Started by rodolfolopes, October 16, 2014, 05:38:16 AM

Previous topic - Next topic

rodolfolopes

Hi there guys!

Im here again asking for help. I have been reading the documentation of MASM32 of the editor but i cant get this one.
I have to ask the user a number for example: 6 and i have to print the multiply table of this number from 1 to 10
the result would be this:

6
12
18
24
30
36
42
48
54
60


Of course of my experience with other languaje i would use a FOR loop
But how you do this with in MASM32

Thanks a lot a lot for your supporte guys :)

dedndave

just maintain a count in a register - ECX is often used as a count value

        xor     eax,eax
        mov     ecx,10

loop00: add     eax,6
        push    ecx
        push    eax
        print   str$(eax),32   ;32 = space
        pop     eax
        pop     ecx
        dec     ecx
        jnz     loop00


EAX, ECX, EDX are volatile (destroyed across calls)
you could use EBX, ESI, or EDI - and eliminate the PUSH/POP, as they are preserved across calls

dedndave


    xor     ebx,ebx
    mov     esi,10
    .repeat
        add     ebx,6
        print   str$(ebx),32
        dec     esi
    .until ZERO?

qWord

include \masm32\include\masm32rt.inc
.code
foo proc
    LOCAL i:DWORD
   
    mov i,1
    .while i <= 10
        imul eax,i,6
        fnc crt_printf,"%d\n",eax
        add i,1
    .endw
   
    ret
   
foo endp

main proc
   
    invoke foo
   
    inkey
    exit
main endp
end main
MREAL macros - when you need floating point arithmetic while assembling!

rodolfolopes

Quote from: dedndave on October 16, 2014, 05:47:57 AM
just maintain a count in a register - ECX is often used as a count value

        xor     eax,eax
        mov     ecx,10

loop00: add     eax,6
        push    ecx
        push    eax
        print   str$(eax),32   ;32 = space
        pop     eax
        pop     ecx
        dec     ecx
        jnz     loop00


EAX, ECX, EDX are volatile (destroyed across calls)
you could use EBX, ESI, or EDI - and eliminate the PUSH/POP, as they are preserved across calls


Hi there Im using your example and i would like to know how to do this
but with the user input ?
Thanks a lot

dedndave


MichaelW

Or you can use the CRT scanf function to read the input, here packaged in a macro similar the MASM32 printf macro (but ANSI only):

;===============================================================================
include \masm32\include\masm32rt.inc
;===============================================================================
scanf MACRO format:REQ, args:VARARG
    IFNB <args>
        fn crt_scanf, cfm$(format), args
    ELSE
        fn crt_scanf, cfm$(format)
    ENDIF
    EXITM <>
ENDM
;===============================================================================
.data
    i32   dd 0
    f     REAL8 ?
    str1  db 24 dup(0)
.code
;=============================================================================== 
start:
;===============================================================================

    printf("Enter an integer:")
    scanf("%d", ADDR i32)
    printf("%d\n",i32)
    printf("Enter a float:")
    ;--------------------------------------------------------------
    ; For floating-point numbers scanf assumes REAL4 (type float),
    ; so to force a conversion to the REAL8 (type double) that
    ; printf expects, you must prefix the type field with "l"
    ; (a lower-case "L", and while upper-case worked in my test
    ; under Windows, lower-case is probably more universal among
    ; CRTs).   
    ;-------------------------------------------------------------- 
    scanf("%lf", ADDR f)   
    printf("%f\n",f)
    printf("Enter string:")
    ;-----------------------------------------------------------
    ; For a string the optional field width (20 in the example)
    ; specifies the maximum number of characters that will be
    ; read from the field.
    ;-----------------------------------------------------------
    scanf("%20s", ADDR str1)   
    printf("%s\n",ADDR str1)   
     
    inkey
    exit
end start
;===============================================================================

Well Microsoft, here's another nice mess you've gotten us into.