News:

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

Main Menu

While loop doesn't works

Started by Oscar, August 05, 2021, 07:40:26 PM

Previous topic - Next topic

Oscar

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.
0x3c21444f43545950452068746d6c3e3c68746d6c3e3c626f64793e3c703e4369616f213c2f703e3c2f626f64793e3c2f68746d6c3e

Mikl__

Hi, Oscar
MOV ECX, 1
.WHILE (ECX <= n)
push ecx;<---
invoke crt_printf, ADDR msg
pop ecx;<---
INC ECX
.ENDW

Oscar

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.
0x3c21444f43545950452068746d6c3e3c68746d6c3e3c626f64793e3c703e4369616f213c2f703e3c2f626f64793e3c2f68746d6c3e

Mikl__

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

Oscar

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...
0x3c21444f43545950452068746d6c3e3c68746d6c3e3c626f64793e3c703e4369616f213c2f703e3c2f626f64793e3c2f68746d6c3e

Mikl__


daydreamer

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
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

K_F

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:
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

hutch--

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

        #

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