powder fresh assembly newbie with sort issues!

I am trying to work on a program that does the following:

three variables are declared in .data section.... NUMS, CHANGES, and SWITCHES

What is to happen then is, first a procedure will sort NUMS in ascending order. Next, the CHANGES variable will switch various things as follows based off the sequence in CHANGES: row, column, change that to this

Then, I honestly have no idea what SWITCHES does... I think it is to work in pairs Row Column, switch with next Row/Column.

But either way, I cant proceed past the point I am currently stuck at.... here is my code..

[code]INCLUDE C:IrvineIrvine16.inc

.data
NUMS dw 09EEBh, 0B0CFh, 061E5h, 089EDh, 0AF17h, 0D8D1h, 06C1Dh, 0594Eh, 0CF55h
dw 03767h, 063C6h, 0AE84h, 0412Fh, 0B226h, 046C1h, 0879Bh, 076B6h, 093FFh
dw 0AFFFh, 05B8Fh, 06164h, 01CF7h, 09A41h, 0A525h, 0A5A1h, 08F05h, 07E4Ch
dw 0827Ah, 090B0h, 0722Dh, 0BCCFh, 033ABh, 0DC76h, 085B6h, 0AA5Fh, 03FB3h
dw 04BACh, 0B822h, 07768h, 0BF1Bh, 05783h, 07EEBh, 09F22h, 0B85Bh, 05312h
dw 05971h, 0B1B6h, 0B16Dh, 054B3h, 073C8h, 0586Bh, 08170h, 06F16h, 092A0h
dw 09680h, 0A23Bh, 0B45Dh, 01E91h, 0415Ah, 0B5D9h, 02D02h, 06748h, 03D39h

CHANGES db 4, 3, 00Ch, 0, 8, 013h, 3, 6, 0CAh, 5, 8, 09Fh, 4, 6, 04Ah
db 0, 3, 0B8h, 5, 2, 0B3h, 1, 3, 0E1h, 5, 5, 09Dh, 4, 1, 00Dh
db 3, 5, 0C1h, 2, 8, 0BFh, 3, 4, 020h, 1, 4, 00Ah, 4, 6, 01Ah
db 1, 5, 0F1h, 2, 3, 0FEh, 1, 5, 03Eh, 6, 3, 0FAh, 0, 5, 008h

SWITCHES db 4, 3, 1, 0, 2, 6, 3, 6, 4, 3, 5, 3, 4, 6, 0, 1, 2, 1, 5, 2
db 5, 4, 5, 6, 5, 5, 4, 6, 1, 4, 3, 5, 1, 3, 2, 7, 3, 4, 2, 2
db 6, 7, 4, 6, 1, 4, 1, 8, 2, 3, 2, 6, 5, 8, 6, 3, 3, 7, 6, 1
db 0, 3, 4, 2, 2, 5, 4, 4, 5, 5, 2, 0, 5, 7, 6, 6, 5, 6, 3, 3


.code
main proc ; control section of program
mov ax,@data ; (if 16-bit program)
mov ds,ax ; (if 16-bit program)
call sorting ; sort each row of NUMS table separately
mov esi,OFFSET NUMS
mov ecx, LENGTHOF NUMS
mov ebx, TYPE NUMS
call DumpMem
;call changing ; read CHANGES, add value to NUMS table
;call switching ; read SWITCHES, exchange NUMS table values
;exit ; quit the program
main endp ; end of control section

sorting proc ; sort each row of NUMS table separately
mov cx,7 ; cx = number of rows
L1: ; outer loop ; walk through the rows
push cx
mov cx,9 ; cx = number of columns
L2: ; middle loop - walk through the columns
push cx
dec cx
mov si,NUMS
L3: ; inner loop - compare and exchange column values
; compare and exchange values from NUMS table vvvvv (down there)
mov ax,[si]
cmp [si+4],ax
jge X
xchg ax,[si+4]
mov [si],ax
X: loop L3 ; end inner loop
pop cx
loop L2 ; end middle loop
pop cx
loop L1 ; end outer loop
ret
sorting endp

changing proc ; read CHANGES, add value to NUMS table
; loop 20 times
; get row, column, number from CHANGES
; calculate NUMS table location
; get word value from NUMS table
; add NUMS table value plus number from CHANGES
; return changed value to NUMS table
; end loop
ret
changing endp

switching proc ; read SWITCHES, exchange NUMS table values
; loop 20 times
; get row 1, column 1 from SWITCHES
; get row 2, column 2 from SWITCHES
; calculate NUMS table location 1 for row 1, column 1
; get value 1 from NUMS table
; calculate NUMS table location 2 for row 2, column 2
; get value 2 from NUMS table
; put value 2 into NUMS table location 1
; put value 1 into NUMS table location 2
; end loop
ret
switching endp

location proc ; given row and column, calculate NUMS location
; calculate NUMS table location from row and column
; absolute location = row * 18 + column * 2
; put location into a register to be used later
ret
location endp
END[/code]

The pseudo code comments were provided for me. Also note, Irvine16.inc is there for the sake of using the DumpMem routine in .main....

I am not sure if my sorting procedure's loop logic is on the right track at all...

any help is appreciated! Thanks for lookin'

--Arkhan

Comments

  • : I am not sure if my sorting procedure's loop logic is on the right
    : track at all...
    :

    It's not doing what it's supposed to be doing. I assume you're planning on using bubble-sort on the list?
    The code you are using constantly compares the first item with the second item. You'll need to advance your position in the NUMS array.

    Perhaps the following will be useful to you. It's a C++ example for bubble sorting (ascending order):
    [code]
    for (int i = NUM_OF_ITEMS-1; i >= 0; i--)
    {
    for (int j = 0; j <= i; j++)
    {
    if (NUMS[j] > NUMS[j+1])
    {
    //This is the C++ equivalent of XCHG
    int temp = NUMS[j];
    NUMS[j] = NUMS[j-1];
    NUMS[j-1] = temp;
    }
    }
    }
    [/code]
    You can translate this to Assembly, and your sorting should work.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : : I am not sure if my sorting procedure's loop logic is on the right
    : : track at all...
    : :
    :
    : It's not doing what it's supposed to be doing. I assume you're
    : planning on using bubble-sort on the list?
    : The code you are using constantly compares the first item with the
    : second item. You'll need to advance your position in the NUMS array.
    :
    : Perhaps the following will be useful to you. It's a C++ example for
    : bubble sorting (ascending order):
    : [code]:
    : for (int i = NUM_OF_ITEMS-1; i >= 0; i--)
    : {
    : for (int j = 0; j <= i; j++)
    : {
    : if (NUMS[j] > NUMS[j+1])
    : {
    : //This is the C++ equivalent of XCHG
    : int temp = NUMS[j];
    : NUMS[j] = NUMS[j-1];
    : NUMS[j-1] = temp;
    : }
    : }
    : }
    : [/code]:
    : You can translate this to Assembly, and your sorting should work.
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry


    I do not really understand how to step through loops like this in assembly....

    I do know that that cx = cx - number of columns statement is supposed to be cx = cx - 1 (aka: dec cx)....

    but i still am having trouble grasping how to step through the loop.
  • [code]for (i = 0; i < 2500; i++);[/code]

    Would resolve to:
    [code]
    mov ecx, 2500
    .Loop:
    nop
    loop .Loop
    [/code]
    Though note that strictly speaking, this assembly loop goes from 2500 to 1, and not from 0 to 2500 (so when using the counter value, you'll want to be careful with this difference)

    Now, a bit more difficult is the nested loop:
    [code]for (i = 0; i < 250; i++) for (j = 0; j < 33; j++) ;[/code]

    Which becomes (again, strictly speaking going from 250 to 1, 33 to 1)
    [code]
    mov ecx, 250
    .OuterLoop:
    push ecx
    mov ecx, 33
    .InnerLoop:
    nop
    loop .InnerLoop
    pop ecx
    loop .OuterLoop
    [/code]

    So what do I mean with being careful when using the counter value?
    Consider the following C code:
    [code]
    int numbers[100];

    for (i = 0; i < 100; i++)
    {
    numbers[i] = i;
    }
    [/code]

    In it's assembly equivalent, you might be tempted to do:
    [code]
    mov ecx, 100
    .Loop:
    mov [numbers + 2*ecx], ecx
    loop .Loop
    [/code]

    This would cause each value to be +1 bigger than the C loop (because of us ending with 1 and the C starting with 0).
    But also, there are times when the direction of the loop matters (eg 1 to 100 would produce a different result from 100 to 1), so then you'll either want to modify the code in the loop (if it's simple) or modify your loop, not using the loop statement.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories