简体   繁体   中英

Why doesn't jl work in asm x86 when by my calculations it should

I'm new to assembly and can't understand why jl decrement_year and jl decrement_month don't seem to work... I'm doing my homework for uni in asm x86 and when comparing the ref to my output the answers are either right or bigger by 1 (they're correct only if I didn't need to use the jumps). This is only part of the program and the result should be a vector of ages calculated from the present day (day/month/year) and a list of birthdays (day/month/year).

    mov edx, dword 0
    mov ebx, dword 1

get_all_ages1:
    mov eax, dword [esi + my_date.day] ; eax = present.day
    sub eax, dword [edi + edx*my_date_size + my_date.day] ; eax -= dates[idx].day

    test eax, eax
    mov eax, dword [esi + my_date.month] ; eax = present.month
    jl decrement_month ; cmp < 0 => eax -= 1

get_all_ages2:
    sub eax, dword [edi + edx*my_date_size + my_date.month] ; eax -= dates[idx].month

    test eax, eax
    mov eax, dword [esi + my_date.year] ; eax = present.year
    jl decrement_year ; cmp < 0 => eax -= 1

get_all_ages3:
    sub eax, dword [edi + edx*my_date_size + my_date.year] ; eax -= dates[idx].year

    test eax, eax
    jle set_age_0 ; if present.year - dates.year <= 0 put 0
    mov [ecx + edx*4], dword eax ; else put eax
    jmp decrement_idx

decrement_idx:
    add edx, 1
    cmp edx, [ebp + 8]
    jl get_all_ages1 ; if idx < len repeat
    jmp stop ; end program

; here I can put any code I don't want used by accident
set_age_0:
    mov [ecx + edx*4], dword 0 ; put 0 at all_ages[idx]
    jmp decrement_idx

decrement_month:
    sub eax, ebx
    jmp get_all_ages2

decrement_year:
    sub eax, ebx
    jmp get_all_ages3
mov eax, dword [esi + my_date.day] mov eax, dword [esi + my_date.month] mov eax, dword [esi + my_date.year]
 sub eax, dword [edi + edx*my_date_size + my_date.day] sub eax, dword [edi + edx*my_date_size + my_date.month] sub eax, dword [edi + edx*my_date_size + my_date.year]

How can these all be dwords ? Is the size of your my_date struct maybe 12? How would using 12 as a scaled index work?

Assuming my_date_size == 4 :

    movzx eax, byte [esi + my_date.day]
    movzx ebx, byte [edi + edx*my_date_size + my_date.day]
    sub   eax, ebx
    ...
    movzx eax, byte [esi + my_date.month]
    movzx ebx, byte [edi + edx*my_date_size + my_date.month]
    sub   eax, ebx
    ...
    movzx eax, word [esi + my_date.year]
    movzx ebx, word [edi + edx*my_date_size + my_date.year]
    sub   eax, ebx

    ...

decrement_month:
    dec   eax
    jmp   get_all_ages2

decrement_year:
    dec   eax
    jmp   get_all_ages3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM