0

The task is to delete rows with zero elements. So, I have this code:

    mov incr, 0
    mov bx, 0
    mov cx, 0
    mov cl, rowsnum
    processexternal:
        mov cl, colsnum
        processinternal:
            cmp matrix[bx], 0 ;compare element to 0
            ;delete row
                    
            inc bx
            
            loop processinternal
        call printnewstr
        mov cl, rowsnum
        mov si, incr
        sub cx, si
        inc incr
        loop processexternal

Here is an iteration on the elements of the matrix. What code should I add?

I think I must have missing something..

bmfdds
  • 3
  • 5
  • How is your matrix stored? If it's a normal 2D array of contiguous storage, you have to copy later rows to fill the space, just like deleting some elements from a 1D array. If you want to support efficient deletion of whole rows, you could use a pointer-based data structure so you just have to copy later row-pointers to fill the gap. It's exactly the same problem as in C, so think about an algorithm there. (e.g. using `memmove`, or `rep movsb` in asm.) – Peter Cordes Apr 05 '22 at 06:29
  • `matrix db 9 * 9 dup (?)` – bmfdds Apr 05 '22 at 06:31
  • Also, what's going on with that crazy logic before the final `loop` instruction? Calculating a new CX value right before `loop` does the equivalent of `dec cx / jnz`? Why not just use a different register for the loop counter like a normal person, like `dec si / jnz outerloop` – Peter Cordes Apr 05 '22 at 06:32
  • well it's subtraction from the total number - how many iterations has already been done – bmfdds Apr 05 '22 at 06:56
  • Ok, so yeah, way overcomplicated vs. just using a separate loop counter. Right now you're effectively using `incr` as the outer loop counter, but instead of checking if it's equal to `rowsnum` with just a `cmp` and a `jne` (after a load if you choose not to keep it in a register), you're duplicating that logic with the `loop` instruction. (And like I said, even better would be `movzx si, byte ptr [rowsnum]` ahead of the loop, and `dec si` `jne outer` at the bottom. (Or if that's an `equ` constant or you don't mind making it a `dw` then just `mov si, rowsnum`. Or use an 8-bit register.) – Peter Cordes Apr 05 '22 at 07:03
  • TL:DR: The `loop` instruction is not the only one that can conditionally jump back to the top of a loop. [It's often the worst choice](https://stackoverflow.com/questions/46881279/how-exactly-does-the-x86-loop-instruction-work/46881622#46881622), when you have to jump through hoops to use it instead of a `jcc`. – Peter Cordes Apr 05 '22 at 07:05

0 Answers0