%include 'rw32-2022.inc'

section .data
    ; 1 + 4 + 2 + 2 + 2 + 4 + 4= 19
    currentTest dd 0
    TEST_REC_SIZE EQU 19
    testCount dd 11
    tests:
    t1  db 10
        dd 20
        dw 10
        dw 7
        dw 7
        dd 0
        dd 0
    t2  db 0
        dd 0
        dw 0
        dw 0
        dw 0
        dd 0
        dd 0
    t3  db 0xFF
        dd 0x0000FFFF
        dw 0xFFFF
        dw 0xFFFF
        dw 0xFFFF
        dd 0
        dd 0
    t4  db 0x80
        dd 0x00008000
        dw 0x8000
        dw 0x8000
        dw 0x8000
        dd 0
        dd 0
    t5  db 0x7F
        dd 0x00007FFF
        dw 0x7FFF
        dw 0x7FFF
        dw 0x7FFF
        dd 0
        dd 0
    t6  db 0x7F
        dd 0x7FFFFFFF
        dw 0x7FFF
        dw 0x7FFF
        dw 0x7FFF
        dd 0
        dd 0
    t7  db 0x80
        dd 0x80000000
        dw 0x8000
        dw 0x8000
        dw 0x8000
        dd 0
        dd 0
    t8  db 0xFF
        dd 0xFFFFFFFF
        dw 0xFFFF
        dw 0xFFFF
        dw 0xFFFF
        dd 0
        dd 0
    t9  db 10
        dd 20
        dw 10
        dw 7
        dw 7
        dd 0xFFFFFFFF
        dd 0xFFFFFFFF
    t10 db 10
        dd 20
        dw 10
        dw 7
        dw 7
        dd 0x7FFFFFFF
        dd 0x7FFFFFFF
    t11 db 10
        dd 20
        dw 10
        dw 7
        dw 7
        dd 0x80000000
        dd 0x80000000

    a db 0
    b dd 0
    c dw 0
    d dw 0
    e dw 0
    q   dd 0
    r   dd 0

section .text
PRINT_ABCDE:
    mov al,'('
    call WriteChar
    mov al,[a]
    call WriteUInt8
    mov al,','
    call WriteChar
    mov eax,[b]
    call WriteUInt32
    mov al,','
    call WriteChar
    mov ax,[c]
    call WriteUInt16
    mov al,','
    call WriteChar
    mov ax,[d]
    call WriteUInt16
    mov al,','
    call WriteChar
    mov ax,[e]
    call WriteUInt16
    mov al,')'
    call WriteChar
    mov al,'='
    call WriteChar
    ret

CMAIN:
    enter 0,0
    pushad
    pushfd

    mov eax,3
     jmp .skip_pars
    mov eax,[ebp+8]     ; argc
    cmp eax,2
    jb exit

    mov esi,[ebp+12]    ; argv
    mov edi,[esi]       ; argv[0]
    CEXTERN atoi
    push dword [esi+4]
    call atoi
    add esp,4
    cmp eax,1
    jb exit
    cmp eax,[testCount]
    ja exit

    mov ebx,-1
    mov ecx,-1
    mov edx,-1
    mov esi,-1
    mov edi,-1
    mov ebp,-1
    .skip_pars:
    imul eax,eax,TEST_REC_SIZE
    lea esi,[tests + eax - TEST_REC_SIZE]
    mov [currentTest],esi
    mov edi,a
    mov ecx,TEST_REC_SIZE
    cld
    rep movsb

    call PRINT_ABCDE

    mov dword [q],0xABABABAB
    mov dword [r],0xBABABABA
    mov eax,[currentTest + 11]
    mov edx,[currentTest + 15]
    mov esi,0
    call task12
    mov esi,q
    mov ecx,2
    call WriteArrayUInt32

exit:
    popfd
    popad
    leave
    xor eax,eax
    ret
user_test_code_begin:
task12:
    xor eax, eax
    xor ebx, ebx
    xor edx, edx
    xor ecx, ecx
    mov al, byte [a]
    cbw
    cwde
    imul dword [b]
    mov ebx, eax
    xor eax, eax
    mov ax, word [c]
    cwde
    add eax, ebx
    add eax, 32
    mov ecx,eax

    xor eax, eax
    xor ebx, ebx
    xor edx, edx
    mov ax, 5
    mov bx, word [d]
    imul bx
    shl edx, 16
    mov dx, ax
    mov eax, edx
    mov ebx, eax
    xor eax, eax
    mov ax, word [e]
    cwde
    add eax, ebx
    mov ebx, 1256
    add eax, ebx

    xchg eax, ecx
    cdq
    idiv ecx
    mov [q], eax
    mov [r], edx
    ret

user_test_code_end:
    ret