Untitled

 avatar
user_8384735
assembly_x86
a year ago
2.3 kB
9
Indexable
Never
TITLE Add and Subtract, Version 2            (AddSub2.asm)
; This program adds and subtracts 32-bit unsigned
; integers and stores the sum in a variable.
INCLUDE Irvine32.inc

.data
    Ans1 BYTE "Yes ", 0
    int_Array DWORD 999, 1, 4
.code
DEBUG PROC uses edx esi ecx ebx
    mov	esi,OFFSET int_array
    mov	ecx,LENGTHOF int_array
    mov	ebx,TYPE int_array   
    call DumpMem
      ret
DEBUG ENDP

DEBUG2 PROC uses edx esi ecx ebx
    mov  edx,OFFSET Ans1
    call WriteString
    ret
DEBUG2 ENDP
QuickSort PROC
    push EBP
    mov EBP, ESP
    push EBX
    push ESI
    push EDI
    mov ESI, [EBP+8]
    mov EAX, [EBP+12]
    mov ECX, 4
    mul ECX
    mov ECX, EAX
    xor EAX, EAX
    mov EBX, ECX
    
    call QuickRecursive
    pop EDI
    pop ESI
    pop EBX
    pop EBP

    RET 
QuickSort ENDP


QuickRecursive PROC
    cmp EAX, EBX
    jge PostIf
   
    push EAX
    push EBX
    add EBX, 4  ;j = high + 1
    mov EDI, [ESI+EAX]
    
    ; call DEBUG
    MainLoop:
        iIncreaseLoop:
            add EAX, 4
            cmp EAX, EBX
            jge End_iIncreaseLoop
            cmp [ESI+EAX], EDI
            jge End_iIncreaseLoop
            jmp iIncreaseLoop
           

        End_iIncreaseLoop:
       
        jDecreaseLoop:
            sub EBX, 4
            cmp [ESI+EBX], EDI
            jle End_jDecreaseLoop
           
            jmp jDecreaseLoop

        End_jDecreaseLoop:
        cmp EAX, EBX
        jge EndMainLoop
       
        push [ESI+EAX]
        push [ESI+EBX]
        
        pop [ESI+EAX]
        pop [ESI+EBX]
        jmp MainLoop
       
    EndMainLoop:       
   
    pop EDI
   
    pop ECX
   
    cmp ECX, EBX
    je EndSwap
   
    push [ESI+ECX]
    push [ESI+EBX]
       
    pop [ESI+ECX]
    pop [ESI+EBX]
       
    EndSwap:
    mov EAX, ECX
    push EDI    ;Saving the high Index
    push EBX    ;Saving j
    sub EBX, 4  ;Setting EBX to j-1
    call QuickRecursive
    pop EAX
    add EAX, 4  ;setting EAX to j+1
    pop EBX
    call QuickRecursive
   
    PostIf:
    RET
QuickRecursive ENDP
main PROC
    mov eax, 0
    push LENGTHOF int_Array - 1
    push OFFSET int_Array
	call QuickSort
    
    call DEBUG
    exit
main ENDP
END main