Software Tuning, Performance Optimization & Platform Monitoring
Discussion around monitoring and software tuning methodologies, Performance Monitoring Unit (PMU) of Intel microprocessors, and platform monitoring

assembly cmp not working as it should after a while

Richard_Son
Beginner
174 Views

I have this assembly code that takes two sorted arrays and merges them to one merged array without repetition:

.global main

.section .text
main:
       lea (array1), %rax
       lea (array2), %rbx
       lea (mergedArray), %rcx
       
loop_hw1_ex3: cmp $0,(%rax) 
              je exit
              cmp $0,(%rbx) 
              je exit
              movl (%rax), %edx
              movl (%rbx), %esi
              
              leaq 4(%rbx), %rdi
              movl (%rdi), %edi
              cmp %esi,%edi # checking if there is repetition in first array 
              jne no_rep_Label_hw1
              leaq 4(%rbx), %rbx
              jmp loop_hw1_ex3
no_rep_Label_hw1:     
              leaq 4(%rax), %rdi
              movl (%rdi), %edi
              cmp %edx,%edi # checking if there is repetition in second array 
              jne no_rep_Label2_hw1
              leaq 4(%rax), %rax
              jmp loop_hw1_ex3
no_rep_Label2_hw1:    
              
              
              cmpl %edx, %esi ## main compare to sort it fails after a while
              jle  less_equal_Label_hw1
              movl %esi, (%rcx)
              leaq  4(%rcx) ,%rcx 
              leaq 4(%rbx), %rbx
              
              jg  bigger_Label_hw1
less_equal_Label_hw1:
              je rax_eq_rbx_label_hw1
              movl %edx, (%rcx)
              leaq  4(%rcx) ,%rcx 
rax_eq_rbx_label_hw1:  # there both arrays have same val so I move one forward
              leaq 4(%rax), %rax
bigger_Label_hw1:
              jmp loop_hw1_ex3
        exit:
  #this the part when one of them has ended and we only need to continue with the other one
loop2_hw1_ex3: cmp $0,(%rbx) 
               je no_loop2 
               movl (%rbx), %esi
               leaq 4(%rbx), %rdi
               movl (%rdi), %edi
               cmp %esi,%edi
               je rep_label_hw1
               movl  %esi, (%rcx)
               leaq 4(%rcx),%rcx
rep_label_hw1: leaq 4(%rbx),%rbx
               jmp loop2_hw1_ex3 
      no_loop2:
loop3_hw1_ex3: cmp $0,(%rax) 
               je no_loop3
               movl (%rax), %edx
               leaq 4(%rax), %rdi
               movl (%rdi), %edi
               cmp %edx,%edi
               je rep_label_hw1
               movl  %edx, (%rcx)
               leaq 4(%rcx),%rcx
rep_label2_hw1:leaq 4(%rax),%rax
               jmp loop3_hw1_ex3 
      no_loop3:

I added the whole code so that maybe you would want to run it on your machine as well. and this is the test I'm doing:

#test
mov $mergedArray, %rax

    
    cmpl $3938153337, 0(%rax)
    jne bad_exit

    cmpl $3921428393, 4(%rax)
    jne bad_exit

    cmpl $3860874895, 8(%rax)
    jne bad_exit

    cmpl $3793010036, 12(%rax)
    jne bad_exit

    cmpl $3728920121, 16(%rax)
    jne bad_exit

    cmpl $3652968237, 20(%rax)
    jne bad_exit

    cmpl $2810665667, 24(%rax)
    jne bad_exit

    cmpl $2734063791, 28(%rax)
    jne bad_exit

    cmpl $2534193645, 32(%rax)
    jne bad_exit

    cmpl $2311096085, 36(%rax)
    jne bad_exit#########3
syscall
bad_exit:
    movq $60, %rax
    movq $1, %rdi
    syscall
.section .data
array1: .int 3938153337, 3860874895, 3793010036, 2810665667, 2734063791, 2311096085, 2083910944, 2062799482, 2029885871, 1979220646, 1968205798, 1855504373, 1824017782, 1629103669, 1437682032, 1364807752, 1269411879, 1167414194, 838087726, 676572428, 595215934, 309552879, 114306948, 6088819, 5996561, 0
array2: .int 3938153337, 3938153337, 3921428393, 3728920121, 3652968237, 2534193645, 2046175771, 1968205798, 1925687059, 1292648006, 165804778, 0
mergedArray: .zero 34

so the code fail when coming to the value 2311096085 , for some reason it keeps considering every other value in the second array bigger and therefore it gets sorted the wrong way, PS: the sorting is from big to small I have debugged it on SASM but I couldn't figure what happened wrong since the values before that were sorted correctly.

0 Kudos
0 Replies
Reply