Author Topic: While loop doesn't works  (Read 531 times)

Oscar

  • Regular Member
  • *
  • Posts: 4
While loop doesn't works
« 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.
Code: [Select]
.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__

  • Member
  • *****
  • Posts: 1135
Re: While loop doesn't works
« Reply #1 on: August 05, 2021, 08:15:28 PM »
Hi, Oscar
Code: [Select]
MOV ECX, 1
.WHILE (ECX <= n)
push ecx;<---
invoke crt_printf, ADDR msg
pop ecx;<---
INC ECX
.ENDW

Oscar

  • Regular Member
  • *
  • Posts: 4
Re: While loop doesn't works
« Reply #2 on: August 05, 2021, 08:19:36 PM »
Hi, Oscar
Code: [Select]
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__

  • Member
  • *****
  • Posts: 1135
Re: While loop doesn't works
« Reply #3 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

Oscar

  • Regular Member
  • *
  • Posts: 4
Re: While loop doesn't works
« Reply #4 on: August 05, 2021, 08:43:28 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__

  • Member
  • *****
  • Posts: 1135
Re: While loop doesn't works
« Reply #5 on: August 05, 2021, 08:55:29 PM »
Welcome to forum section Mikl__'s ml64 examples

daydreamer

  • Member
  • *****
  • Posts: 1762
  • building nextdoor
Re: While loop doesn't works
« Reply #6 on: August 05, 2021, 09:19:21 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
SIMD fan and macro fan
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."

K_F

  • Member
  • *****
  • Posts: 1673
  • Anybody out there?
Re: While loop doesn't works
« Reply #7 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:
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8568
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: While loop doesn't works
« Reply #8 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

        #

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: