1

I implemented all the "for" loops and I have an infinite cycle. I tried my best, but I can not find the problem, please help. I just want my loops to work. Please help. This is the code


.data

m1: .long 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0
m2: .long 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0
mres: .long 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
n: .long 4
fs: .asciz "%ld"

.text

matrix_mult:

    pushl %ebp
    mov %esp, %ebp
    
    pushl %edi
    pushl %esi
    pushl %ebx
    
    subl $12, %ebp
    // -16(ebp) = i
    // -20(ebp) = j
    // -24(ebp) = k
    
    subl $8, %ebp
    // -28(ebp) = x
    // -32(ebp) = y locul unde stocam elem din cele 2 matrici
    movl $0, -16(%ebp)
    movl $0, -20(%ebp)
    movl $0, -24(%ebp)
    
    et_for1:
        
        mov -16(%ebp), %eax
        mov 20(%ebp), %ecx
        cmp %eax, %ecx
        jge final
        movl $0, -20(%ebp)
            et_for2:
            
                mov -20(%ebp), %eax
                mov 20(%ebp), %ecx
                cmp %eax, %ecx
                jge cont_for2
                movl $0, -24(%ebp)
                    et_for3:
                        mov -24(%ebp), %eax
                        mov 20(%ebp), %ecx
                        cmp %eax, %ecx
                        jge cont_for3
                        
                        //mres[i][j]=m1[i][k]*m2[k][j]
                        lea 8(%ebp), %edi
                        lea 12(%ebp), %esi
                        //ecx=i*n+k, ecx= indicele pt m1
                        mov -16(%ebp), %eax
                        xorl %edx, %edx
                        mull 20(%ebp)
                        addl -24(%ebp), %eax
                        mov %eax, %ecx
                        //eax=k*n+j, eax= indicele pt m2
                        mov -24(%ebp) , %eax
                        xorl %edx, %edx
                        mull 20(%ebp)
                        addl -20(%ebp), %eax
                        
                        //ecx = produsul termenilor
                        mov (%edi, %ecx, 4), %ebx
                        mov %ebx, -28(%ebp)
                        mov (%esi, %eax, 4), %ebx
                        mov %ebx, -32(%ebp)
                        mov -28(%ebp), %eax
                        xorl %edx, %edx
                        mull -32(%ebp)
                        mov %eax, %ecx
                        
                        //eax=i*n+j
                        movl -16(%ebp), %eax
                        xorl %edx, %edx
                        mull 20(%ebp)
                        movl -20(%ebp), %ebx
                        addl %ebx, %eax
                        //ecx= prod term + mres[i][j]
                        lea 16(%ebp), %edi
                        movl (%edi, %eax, 4), %ebx
                        movl %ebx, -28(%ebp)
                        addl -28(%ebp), %ecx
                        
                        movl %ecx, (%edi, %eax, 4)
                    
                    mov -24(%ebp), %ebx
                    incl %ebx
                    mov %ebx, -24(%ebp) 
                    //k++
                    jmp et_for3
            cont_for3:
                
            mov -20(%ebp), %ebx
            incl %ebx
            mov %ebx, -20(%ebp) 
            mov $0, -24(%ebp)
            //j++ si k=0
            jmp et_for2 
                    
    
    cont_for2:
    
        
    mov -16(%ebp), %ebx
    incl %ebx
    mov %ebx, -16(%ebp) 
    //i++
    jmp et_for1
    
    final:
        addl $20, %esp
        popl %ebx
        popl %esi
        popl %edi
        popl %ebp
        ret

.global main main:

pushl n
pushl mres
pushl m2
pushl m1
call matrix_mult
addl $16, %esp




lea mres, %edi
mov $0, %ecx
mov n, %eax
xorl %edx, %edx
mull %eax 
//eax=n*n

et_for:
    
    cmp %eax, %ecx
    je et_exit
    
    mov (%edi, %ecx, 4), %ebx
    pushl %eax
    pushl %ecx

    
    //afisare ebx
    push %ebx
    push $fs
    call printf
    addl $8, %esp
    
    push $0
    call fflush
    addl $4, %esp
    
    
    popl %ecx
    popl %eax
    incl %ecx
    
jmp et_for
    
et_exit:

mov $1, %eax
xorl %ebx, %ebx
int $0x80

I tried to print some values in order to understand better what s happening...but I can t find the problem

  • 1
    Best advice I can give is learn to use a debugger and step through the code to find out where/why things may be going wrong. – Michael Petch Dec 26 '22 at 13:04
  • You don't need to zero EDX ahead of `mull`, it's a write-only output for one-operand `mul`. If you're not going to use that output, use the more efficient 2-operand form of multiply, like `imul (%esi), %ebx` or whatever. – Peter Cordes Dec 26 '22 at 20:08
  • 1
    It looks super weird that your function is multiplying a value from `20(%ebp)`, a function arg. Did you pass the whole matrix by value? No, you passed the first element of each with `push m1` instead of passing a pointer with `push $m1`. See [What's difference between number with $ or without $ symbol in at&t assembly syntax?](https://stackoverflow.com/q/18996870) Oh also you did `subl $12, %ebp`; normally you'd move ESP to make space for locals, not adjust EBP. Anyway, like Michael Petch said, use a debugger to single-step and look at register values. – Peter Cordes Dec 26 '22 at 20:09
  • Also, when looping over a row/column dot-product, keep the total in a register, not storing/reloading to memory. – Peter Cordes Dec 26 '22 at 20:11

0 Answers0