News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

HeapSort

Started by mich10892, January 16, 2015, 07:51:31 AM

Previous topic - Next topic

mich10892

Hello Everyone,

This is my first message on this forum.
I must write a program in MASM32:
Program  load to array 1000 random nubmers and sorting this number by heapsort alghoritm.
My question is How to implement heapsort in MASM32?

jj2007

Hello mich10892,

First things first: Welcome to the forum :icon14:

Now the bad news: There is a "no homework" rule in place here, so nobody will do homework for you. However, if you demonstrate that you are willing to do your work, e.g. by
- studying the heapsort wiki
- coding one variant into a language of your choice (Java, Basic, C, ...)
- make significant efforts to translate your code to assembler
- and posting all that in a nice zip archive,

then certainly we will help you to get it running. Now isn't that a nice offer? When is your deadline?

mich10892

Ok,
I write code in assembly, but in 127 line I get error:
error A2006: undefined symbol : DGROUP

.model small
.data
mas db 9,0,6,7,4,1,5,2,8,3
n=$-mas+1
x db 0
i dd 0
l dd 0
m dd 0
j dd 0
k dd 0
ASSUME DS:_DATA
.stack 256
.586
.code
insert_item_in_tree proc
push esi
push cx
mov j,esi
@@m4:
mov esi,j
mov eax,j
shl eax,1
mov k,eax
inc eax
mov l,eax
cmp eax,m
ja @@m2
mov al,mas[esi-1]
mov edi,k
cmp al,mas[edi-1]
jna @@m6
inc edi
cmp al,mas[edi-1]
jna @@m6
jmp @@m2
@@m6:
mov edi,k
mov al,mas[edi-1]
inc edi
cmp al,mas[edi-1]
jna @@m3
mov al,mas[esi-1]
mov x,al
dec edi
mov al,mas[edi-1]
mov mas[esi-1],al
mov j,edi
mov al,x
mov mas[edi-1],al
jmp @@m4
@@m3:
mov al,mas[esi-1]
mov x,al
mov al,mas[edi-1]
mov mas[esi-1],al
mov j,edi
mov al,x
mov mas[edi-1],al
jmp @@m4
@@m2: ;
mov eax,k
cmp eax,m
jne @@m1
mov edi,k
mov al,mas[edi-1]
cmp mas[esi-1],al
jae @@m1
mov al,mas[esi-1]
mov x,al
mov edi,n
mov al,mas[edi-1]
mov mas[esi-1],al
mov al,x
mov mas[edi-1],al
@@m1:
pop cx
pop esi
ret
insert_item_in_tree endp
main:
mov dx,@data
mov ds,dx
mov cx,0
mov ah,02h
lea esi,mas
inmas:
mov dl,[esi]
add dl,30h
int 21h
inc cx
inc esi
cmp cx,10
jb inmas
mov dx,0Ah
int 21h
mov dx,0Dh
int 21h
mov eax,n
mov m,eax
shr eax,1
mov i,eax
mov l,eax
mov ecx,l
mov esi,i
@@cycl1:
mov i,esi
call insert_item_in_tree
dec esi
loop @@cycl1
mov cx,n-1
@@cycl2:
xor esi,esi
mov al,mas[esi]
mov x,al
mov edi,m
mov al,mas[edi-1]
mov mas[esi],al
mov al,x
mov mas[edi-1],al
dec m
inc esi
call insert_item_in_tree
loop @@cycl2
mov cx,0
mov ah,02h
lea esi,mas
outmas:
mov dl,[esi]
add dl,30h
int 21h
inc cx
inc esi
cmp cx,10
jb outmas
mov eax,4c00h
int 21h
end


jj2007

Quote from: mich10892 on January 17, 2015, 08:31:44 AMI write code in assembly, but in 127 line I get error:
error A2006: undefined symbol : DGROUP

You have written 16-bit code, therefore you need to use the 16-bit linker. Adjust your project options accordingly: instead of \Masm\bin\link.exe, you will need link16.exe

mich10892

How can I translate this code to 32-bit?

jj2007

Quote from: mich10892 on January 17, 2015, 11:46:43 PM
How can I translate this code to 32-bit?

Where did you find it? It looks like an odd mix of 16- and 32-bit code anyway.
I might help you if the code was properly commented, i.e.
  nop   ; this instruction does nothing
behind each and every line.

K_F

mich10892: If you want to be a Software engineer of note, and get high marks, this is a pointer of what your must do

I know, I worked in Electrical Engineering marking student assignments :)
Also don't try to copy others work... you will be caught :) :)

Paragraph your code as if you're writing a book...
ie: seperate sections that do different functions/things with blank lines, placing comments describing what the section does or on the program flow.

Excuse the text alignment/formatting... html editors are a pain.

;------------------------------------------------
; HEAP SORT PROGRAM BY 'YOURS TRULY'
;
;ASSIGNMENT XX FOR COMPUTER SCIENCE COURSE 101
;Date: 35/67/20156
:
;(Maybe a note of Sorting and Heap Sort)
;------------------------------------------------

;Small data model used as this is a small program
.model small

;Data section containing initialised variables
.data
mas db 9,0,6,7,4,1,5,2,8,3 ;Array of Bytes to be Heap sorted
n=$-mas+1

x db 0 ;Give meaningful names to your variables
i dd 0 ;so that your assessor can understand what
l dd 0 ;they're all about !!
m dd 0 ;
j dd 0 ;
k dd 0 ;

ASSUME DS:_DATA ;DS Segment register points to .data section

.stack 256 ;Initialise stack to 256 bytes

.586 ;Target CPU instruction codes

; Start of code section
.code

;---PROCEDURE-----------------
;insert_item_in_tree:
;
;This Procedure does.....
;-----------------------------
insert_item_in_tree proc

push esi ;PROLOG
push cx ;Save used registers ..
mov j,esi ;

;--- Why we're here ---
@@m4:
mov esi,j ;General Comments on every line !!
mov eax,j ;If you don't do this and come back a week later
shl eax,1 ;you will have forgotten what you've done...
mov k,eax ;
inc eax ;Commenting helps you/others to re-understand faster
mov l,eax ;
cmp eax,m ;
ja @@m2 ;

mov al,mas[esi-1]
mov edi,k
cmp al,mas[edi-1]
jna @@m6

inc edi
cmp al,mas[edi-1]
jna @@m6
jmp @@m2

;--- Why we're here ---
@@m6:
mov edi,k
mov al,mas[edi-1]
inc edi
cmp al,mas[edi-1]
jna @@m3

mov al,mas[esi-1]
mov x,al
dec edi
mov al,mas[edi-1]
mov mas[esi-1],al
mov j,edi
mov al,x
mov mas[edi-1],al
jmp @@m4

;--- Why we're here ---
@@m3:
mov al,mas[esi-1]
mov x,al
mov al,mas[edi-1]
mov mas[esi-1],al
mov j,edi
mov al,x
mov mas[edi-1],al
jmp @@m4

;--- Why we're here ---
@@m2: ;
mov eax,k
cmp eax,m
jne @@m1
mov edi,k
mov al,mas[edi-1]
cmp mas[esi-1],al
jae @@m1

mov al,mas[esi-1]
mov x,al
mov edi,n
mov al,mas[edi-1]
mov mas[esi-1],al
mov al,x
mov mas[edi-1],al

;--- Why we're here ---
@@m1:
pop cx ;PROLOG
pop esi ;
ret

;---PROCEDURE-----------------
;insert_item_in_tree:
;
;This Procedure does.....
;-----------------------------
insert_item_in_tree endp
main:
mov dx,@data
mov ds,dx
mov cx,0
mov ah,02h
lea esi,mas

;--- Why we're here ---
inmas:
mov dl,[esi] ; Give meaningful information on the
add dl,30h ; DOS function and what it's doing
int 21h ;

inc cx
inc esi
cmp cx,10
jb inmas
mov dx,0Ah
int 21h

mov dx,0Dh
int 21h

mov eax,n
mov m,eax
shr eax,1
mov i,eax
mov l,eax
mov ecx,l
mov esi,i

;--- Why we're here ---
@@cycl1:
mov i,esi
call insert_item_in_tree
dec esi
loop @@cycl1
mov cx,n-1

;--- Why we're here ---
@@cycl2:
xor esi,esi
mov al,mas[esi]
mov x,al
mov edi,m
mov al,mas[edi-1]
mov mas[esi],al
mov al,x
mov mas[edi-1],al
dec m
inc esi
call insert_item_in_tree
loop @@cycl2

mov cx,0
mov ah,02h
lea esi,mas

;--- Why we're here ---
outmas:
mov dl,[esi]
add dl,30h
int 21h
inc cx
inc esi
cmp cx,10
jb outmas

;--- Why we're here ---
mov eax,4c00h ;DOS FUNCTION -> EXIT PROGRAM WITH NO EXIT CODE !!
int 21h ;
end
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'