0

My question is why isn't cmp comparing my bits properly or the data from array in the right way?

I am open too comparing the data from the array with the limits a different way such as cmpsd or repe cmpsd any help is appreciated.?

Can anyone show me what I am doing wrong here within my code?

;Sum Array Integers within Limits

Include Irvine32.inc

.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
lowerLimit dword 20
upperLimit dword 40
array_1 sdword 10,30,25,15,17,19,40,41,43
array_2 sdword 10,-30,25,15,-17,55,40,41,43

.code
main Proc

;Array
    mov edi, 0                          ;clear register
    mov ebx, 0                          ;clear register
    mov ecx, 0                          ;clear register
    mov esi, 0                          ;clear register
    mov esi, OFFSET array_1             ;set buffer in array_1
    mov ecx, LENGTHOF array_1           ;set ecx register to length of array for counter
    mov edi, lowerLimit                 ;set buffer to lower limit for comparison

    L1:
        cmp esi, esi                            ;compare bytes
        jge lower                       ;jump to lower label if greater or equal to 
        jmp next                        ;jump to next label

    lower:
            
            mov edi, 0                  ;clear register
            mov edi, upperLimit         ;set edi to upperlimit
            cmp edi, esi                ;compare bits in register 
            jle addele                  ;jump if less than equal to upper limit
            jmp next                    ;jump to next label if not less than or equal to
    addele:                             
            add ebx, [esi]              ;add element to ebx sum register from esi
            jmp next                    ;go to next label
    next:
            add esi, TYPE array_1       ;increment esi by 4 

    loop L1                             ;loop

;Array_two
    mov eax, 0
    mov ecx, 0 
    mov esi, 0
    mov esi, OFFSET array_2
    mov ecx, LENGTHOF array_2
    mov edi, lowerLimit

    L2:
        cmp edi, esi
        jge lo2
        jmp next2

    lo2: 
            mov edi,0
            mov edi,  upperLimit
            cmp edi, esi
            jle addele2
            jmp next2
    addele2:
            add eax, [esi]
            jmp next2
    next2:
            add esi, TYPE array_2
    loop L2

    call Crlf                               ;move to next line
    call WriteInt
    call Crlf
    mov eax,0
    mov eax,ebx
    call WriteInt
    
            

    INVOKE ExitProcess,0
main ENDP
END main

Above is my code.

Henry Lj
  • 1
  • 1
  • 1
    You seem to not realize the difference between **value** and **pointer**. Comparision should be for instance `L1: cmp [esi],edi ; Compare the dword addressed by esi with the value loaded in edi.` BTW you don't have to *clear register* when the following `mov` overwrites the *cleared* value in it. – vitsoft Jul 27 '22 at 10:18
  • Almost a duplicate of [Why do I need to use \[ \] (square brackets) when moving data from register to memory, but not when other way around?](https://stackoverflow.com/q/27923915) or [Basic use of immediates vs. square brackets in YASM/NASM x86 assembly](https://stackoverflow.com/q/10362511) since it works the same way for brackets (or not) around registers. NASM differs from your MASM syntax when using bare symbol names like `mov edi, upperLimit` being a load in MASM syntax, same as `mov edi, [upperLimit]` which is more explicit. Ideally load both limits before the loop into different regs. – Peter Cordes Jul 27 '22 at 17:13
  • Thank you for your help! My previous assembly x86 teacher said it is good to zero out registers you will be using because they may contain garbage data?. I will review the brackets! – Henry Lj Jul 28 '22 at 01:02
  • This is true only when you load only partial subregister rather then its entire contents, for instance `mov ax,0` `mov al,[ByteValue]`. Anyway, it's more effective to use `movzx ax,[ByteValue]` in such case. – vitsoft Jul 28 '22 at 06:34

0 Answers0