Blogger news

Followers

Friday, February 21, 2014


ASSUME DS: DATA, CS: CODE
DATA SEGMENT                                       ; start of data segment
LIST  DB 12H,10H,04H,08H                          ;  n (4) number of elements stored in a LIST array
COUNT DB 03H                                              ; n-1 counter value stored in COUNT  variable
DATA ENDS                                                 ; end of data segment
CODE SEGMENT                                       ; start of code segment
START : MOV AX, DATA                ; initializing the data segment
   MOV DS, AX
   XOR AX, AX                     ; clear AX register content
   MOV CL, COUNT             ; store n-1 value (3) in CL register (outer loop count)
BACK2:  MOV SI, OFFSET LIST    ; set the pointer to the 1st location of LIST array
   MOV BL, CL                      ; temporally store the n-1 value in BL register (inner loop count)
BACK1:   MOV AL, [SI]                     ; get the 1st array element, ie. LIST [0] to AL
    INC SI                                ; increment the pointer by 1 to point to the next location 
                                    ; ie: LIST[SI+1]
   CMP AL, [SI]                       ; compare AL with LIST[SI+1]
   JB NEXT                                     ; if 1st element is less than 2nd element then go to NEXT to
; decrement the inner loop count value in BL, to indicate one
; comparison is over.
   XCHG  [SI], AL                  ; otherwise swap the numbers.
   MOV   [SI-1], AL
NEXT:    DEC BL                               ; decrement inner loop count value.
   JNZ BACK1                          ; if inner loop count value is not 0, then go to BACK1 to  
; repeat the comparison of remaining elements of LIST array.

   LOOP BACK2                              ; decrement the outer loop count value in CL register
; (perform n-1  passes)
   MOV AH, 4CH                   ; terminate the program
   INT 21H
CODE ENDS                                                 ; end of code segment
END START



Output:
D:\MASM> Masm 3a.asm;
D:\MASM> Link 3a.obj;

D:\MASM> TD 3a.exe
Then press F7 to trace the program (line by line execution).
Click on View menu and select Dump to see the output.

2 comments:

Unknown said...

Too gud

Unknown said...

before the loop back2 command, there must be command to decrement CL