News:

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

Main Menu

Nested 'for' loop implementation using .while...

Started by zedd151, January 18, 2025, 04:28:05 PM

Previous topic - Next topic

zedd151

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.
¯\_(ツ)_/¯

daydreamer

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

TimoVJL

#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;
}
May the source be with you

NoCforMe

Assembly language programming should be fun. That's why I do it.

jj2007

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:

TimoVJL

May the source be with you

sinsi

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:

jj2007

By definition, a source is Assembly if it can be assembled :biggrin:

daydreamer

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


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

sinsi


jj2007

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 ;-)

zedd151

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.
¯\_(ツ)_/¯

NoCforMe

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.
Assembly language programming should be fun. That's why I do it.

zedd151

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:
¯\_(ツ)_/¯

NoCforMe

Let me know when you've got it done. And it actually works.
Assembly language programming should be fun. That's why I do it.