The MASM Forum

General => The Campus => Topic started by: Oscar on August 05, 2021, 07:40:26 PM

Title: While loop doesn't works
Post by: Oscar on August 05, 2021, 07:40:26 PM
From what I learnt in MASM is possible to use while loop without having to deal with CMP and conditional jump. But when I try to use it, the while is not used and the program ends.

.386
.model flat, stdcall
option casemap:NONE

include \masm32\include\masm32rt.inc

.data
msgInput DB "Insert a positive integer:", 0Ah, 0
msg DB "Loop", 0Ah, 0
n DD ?

.code
start:
invoke crt_printf, ADDR msgInput
invoke crt_scanf, chr$("%d"), ADDR n

MOV ECX, 1
.WHILE (ECX <= n)
invoke crt_printf, ADDR msg
INC ECX
.ENDW

invoke ExitProcess, 0
end start

In this program I get an integer n from the user and then print n times the string "Loop". From my understanding is releated to the value of ECX register, but in the code I just use INC to increase it by one each loop.
Title: Re: While loop doesn't works
Post by: Mikl__ on August 05, 2021, 08:15:28 PM
Hi, Oscar
MOV ECX, 1
.WHILE (ECX <= n)
push ecx;<---
invoke crt_printf, ADDR msg
pop ecx;<---
INC ECX
.ENDW
Title: Re: While loop doesn't works
Post by: Oscar on August 05, 2021, 08:19:36 PM
Quote from: Mikl__ on August 05, 2021, 08:15:28 PM
Hi, Oscar
MOV ECX, 1
.WHILE (ECX <= n)
push ecx;<---
invoke crt_printf, ADDR msg
pop ecx;<---
INC ECX
.ENDW

Thank you it works. But I don't understand why you putted in the stack the value of ECX register.
Title: Re: While loop doesn't works
Post by: Mikl__ on August 05, 2021, 08:40:19 PM
Hi, Oscar!
In accordance with the calling convention, the registers EAX, EDX, ECX are allowed to be changed inside functions, but EBX, ESI, EDI, EBP are not. You can either save and restore ECX (push ecx / pop ecx), or use, for example, EBP
Title: Re: While loop doesn't works
Post by: Oscar on August 05, 2021, 08:43:28 PM
Quote from: Mikl__ on August 05, 2021, 08:40:19 PM
Hi, Oscar!
In accordance with the calling convention, the registers EAX, EDX, ECX are allowed to be changed inside functions, but EBX, ESI, EDI, EBP are not. You can either save and restore ECX (push ecx / pop ecx), or use, for example, EBP
Oh thank you for the explanation. I have a lot to learn...
Title: Re: While loop doesn't works
Post by: Mikl__ on August 05, 2021, 08:55:29 PM
Welcome to forum section Mikl__'s ml64 examples (http://masm32.com/board/index.php?board=54.0) (http://www.cyberforum.ru/images/smilies/good3.gif)
Title: Re: While loop doesn't works
Post by: daydreamer on August 05, 2021, 09:19:21 PM
Quote from: Oscar on August 05, 2021, 08:43:28 PM
Quote from: Mikl__ on August 05, 2021, 08:40:19 PM
Hi, Oscar!
In accordance with the calling convention, the registers EAX, EDX, ECX are allowed to be changed inside functions, but EBX, ESI, EDI, EBP are not. You can either save and restore ECX (push ecx / pop ecx), or use, for example, EBP
Oh thank you for the explanation. I have a lot to learn...
Welcome Oscar.
Read all help files from qeditor, first.  Read masm intro,that explains alot of basic things like registers,flags,stack,calling convention, when you want to use while,ifs etc check macro help file
So you can use print,loc(set cursor position) ,cls(clear screen) macros in console if you prefer macros over invoke printf
Title: Re: While loop doesn't works
Post by: K_F on August 06, 2021, 03:32:46 AM
Welcome to the new order.. :biggrin:

Second DayDreamers comment.

The best method is to get to know what makes the engine tick.
Take your time with the reading material - You'll find yourself reading it many times, each time reveals something different.

You'll never be sorry that you've ventured done this 'rabbit hole'.  :mrgreen:
:thumbsup:
Title: Re: While loop doesn't works
Post by: hutch-- on August 06, 2021, 10:50:56 AM
Here is a simple example using some basic mnemonics.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL cnt   :DWORD
    LOCAL var   :DWORD

    mov cnt, 10                     ; set the iterations for the loop
    mov var, 0                      ; set start of display numbers

  lbl1:
    add var, 1
    invoke StdOut,str$(var)         ; display the number
    invoke StdOut,chr$(13,10)       ; append a CRLF after each number
    sub cnt, 1                      ; sub the counter by 1
    jnz lbl1                        ; loop back if its not 0

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start

comment #

        the SUB instruction sets the ZERO flag so you don't need to do a CMP

        #

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤