The MASM Forum

General => The Workshop => Topic started by: zedd151 on January 18, 2025, 04:28:05 PM

Title: Nested 'for' loop implementation using .while...
Post by: zedd151 on January 18, 2025, 04:28:05 PM
Nested 'for' loop implemented in asm using '.while' loops.  :biggrin:

C code implementation: a console program.
#include <stdio.h>

int main() {
    int row, col;

    for (row = 1; row <= 9; row++) {
        for (col = 1; col <= 9; col++) {

            printf("r%d", row);
            printf("c%d", col);
            printf("\n");

        }
    }

    return 0;
}

asm code implementation: assemble and link as console program.
include \masm32\include\masm32rt.inc

.data

    row dd 0
    col dd 0

.code

start proc

    mov row, 0
  .while row < 9
    inc row
    mov col, 0
  .while col < 9
    inc col

    fn crt_printf, "r%d" , row
    fn crt_printf, "c%d" , col
    fn crt_printf, chr$(13, 10)
   
  .endw
  .endw
 
    inkey
    exit

start endp
end start
This code might be useful for anyone converting C code to masm compatible assembly.


Both .asm example and .C example  :tongue:  attached, for comparison.
Title: Re: Nested 'for' loop implementation using .while...
Post by: daydreamer on January 18, 2025, 04:51:33 PM
Isn't uasm supporting for loops ?
Title: Re: Nested 'for' loop implementation using .while...
Post by: TimoVJL on January 18, 2025, 04:55:51 PM
#include <stdio.h>

int main(void) {
    int row, col;
    row = 0;
    while (row++ <= 9) {
col = 0;
while (col++ <= 9) {
            printf("r:%d c:%d\n", row, col);
}
    }
    return 0;
}
Title: Re: Nested 'for' loop implementation using .while...
Post by: NoCforMe on January 18, 2025, 05:35:23 PM
Always with the C ...
Title: Re: Nested 'for' loop implementation using .while...
Post by: jj2007 on January 18, 2025, 06:44:12 PM
include \masm32\MasmBasic\MasmBasic.inc
  Init
  For_ row=1 To 9
    For_ col=1 To 9
        Print Str$("r%i", row), Str$("c%i ", col)
    Next
    Print
  Next
EndOfCode
r1c1 r1c2 r1c3 r1c4 r1c5 r1c6 r1c7 r1c8 r1c9
r2c1 r2c2 r2c3 r2c4 r2c5 r2c6 r2c7 r2c8 r2c9
r3c1 r3c2 r3c3 r3c4 r3c5 r3c6 r3c7 r3c8 r3c9
r4c1 r4c2 r4c3 r4c4 r4c5 r4c6 r4c7 r4c8 r4c9
r5c1 r5c2 r5c3 r5c4 r5c5 r5c6 r5c7 r5c8 r5c9
r6c1 r6c2 r6c3 r6c4 r6c5 r6c6 r6c7 r6c8 r6c9
r7c1 r7c2 r7c3 r7c4 r7c5 r7c6 r7c7 r7c8 r7c9
r8c1 r8c2 r8c3 r8c4 r8c5 r8c6 r8c7 r8c8 r8c9
r9c1 r9c2 r9c3 r9c4 r9c5 r9c6 r9c7 r9c8 r9c9

Purest Assembly :cool:
Title: Re: Nested 'for' loop implementation using .while...
Post by: TimoVJL on January 18, 2025, 07:08:42 PM
Quote from: jj2007 on January 18, 2025, 06:44:12 PMPurest Assembly :cool:
or pure MACROs  :toothy:
Title: Re: Nested 'for' loop implementation using .while...
Post by: sinsi on January 18, 2025, 07:43:09 PM
Why is something so basic in the Workshop?


Quote from: jj2007 on January 18, 2025, 06:44:12 PMPurest Assembly :cool:
If I squint really hard...nope, still can't see any :tongue:
Title: Re: Nested 'for' loop implementation using .while...
Post by: jj2007 on January 18, 2025, 08:25:19 PM
By definition, a source is Assembly if it can be assembled :biggrin:
Title: Re: Nested 'for' loop implementation using .while...
Post by: daydreamer on January 18, 2025, 09:47:36 PM
Computer parts Assembly to computer makes it assembler ?:)

My version
;nested for loops
.data
array 0-3
sqarray 0-3
primearray 0-3

.code
for y=0 to 3
sqrarray[y]=sqr(array[y])
for x=0 ,x<sqrarray[y]
if array[y] mod == 0 primearray[y] =false else primearray[y]=true;break;
end for x
end for y


Title: Re: Nested 'for' loop implementation using .while...
Post by: sinsi on January 18, 2025, 09:48:34 PM
Quote from: jj2007 on January 18, 2025, 08:25:19 PMBy definition, a source is Assembly if it can be assembled :biggrin:
Like Ikea?
Title: Re: Nested 'for' loop implementation using .while...
Post by: jj2007 on January 18, 2025, 10:30:26 PM
Quote from: sinsi on January 18, 2025, 09:48:34 PM
Quote from: jj2007 on January 18, 2025, 08:25:19 PMBy definition, a source is Assembly if it can be assembled :biggrin:
Like Ikea?
Exactly :biggrin:

The only difference is that, unlike Ikea stuff, my sources assemble with MASM ;-)
Title: Re: Nested 'for' loop implementation using .while...
Post by: zedd151 on January 19, 2025, 01:26:35 AM
Thanks guys  :badgrin:

Thinking about the code presented, it wouldn't be very hard at all to write a qeditor plugin to do at least this part of a C-->asm conversion.  :azn:

The other parts (code within the for loop) will depend... may still need manual tweaks for that bit.
Title: Re: Nested 'for' loop implementation using .while...
Post by: NoCforMe on January 19, 2025, 02:34:10 AM
Quote from: zedd151 on January 19, 2025, 01:26:35 AMThinking about the code presented, it wouldn't be very hard at all to write a qeditor plugin to do at least this part of a C-->asm conversion.  :azn:

All I can say is rotsa ruck with that.
Title: Re: Nested 'for' loop implementation using .while...
Post by: zedd151 on January 19, 2025, 02:57:15 AM
Quote from: NoCforMe on January 19, 2025, 02:34:10 AMAll I can say is rotsa ruck with that.
Well, thanks man!  :thumbsup:

The code has already been started.  :cool:
Title: Re: Nested 'for' loop implementation using .while...
Post by: NoCforMe on January 19, 2025, 03:12:37 PM
Let me know when you've got it done. And it actually works.
Title: Re: Nested 'for' loop implementation using .while...
Post by: daydreamer on January 19, 2025, 09:06:03 PM
Quote from: zedd151 on January 19, 2025, 02:57:15 AM
Quote from: NoCforMe on January 19, 2025, 02:34:10 AMAll I can say is rotsa ruck with that.
Well, thanks man!  :thumbsup:

The code has already been started.  :cool:
Good luck zedd  :thumbsup:
Title: Re: Nested 'for' loop implementation using .while...
Post by: zedd151 on January 20, 2025, 05:58:08 AM
Quote from: daydreamer on January 19, 2025, 09:06:03 PMGood luck zedd  :thumbsup:
:smiley:  It will take a little while, to get the code 'just right' to enable converting multi-nested for loops. Plus I always get sidetracked (real life, etc.), so never really easy to finish projects that I start.
Title: Re: Nested 'for' loop implementation using .while...
Post by: NoCforMe on January 20, 2025, 06:14:16 AM
I call the place where I live "The House of Unfinished Projects".
Title: Re: Nested 'for' loop implementation using .while...
Post by: daydreamer on January 22, 2025, 04:27:06 AM
Quote from: NoCforMe on January 20, 2025, 06:14:16 AMI call the place where I live "The House of Unfinished Projects".
I agree,probably similar home here but in Europe ,but I am looking on the bright side,even unfinished projects going into new area, gui for example makes you more skilled coder while having fun