; first you need to start off and declare your variables and your proccessor/model
; i use model medium because it is good for small projects and easy to work with
; ill use tasm code but it should be very easy to convert to masm (or nasm)
.model medium
.386 ;386 instructions should be sufficient
.stack 500h ; a good size stack for recursions might need to make this bigger
.data ; declair your data section (if you are using tasm, otherwise it is different)
; put all of your variables here that you will use throught the program
lp dw ? ;you use (variable name) (size) (value) form
rp dw ? ;use question mark to set as undefined
v dw ? ; you might want to use db (declare byte) here instead of dw (declare word)
right dw ?
left dw ?
x dw ?
y dw ?
temp dw ?
a dw ? dup (?) ; this is an array with ? sets of ? (i dont know if this will work right, it
; might be safer to put 100 dup (?) just so the assembler knows how
; many blocks there are, but you can try it either way.
;functions here----------------------
quicksort proc ; name of procedure followed by 'proc'
;im not sure exactly how your code works so i will try my best to convert it
; in asm you dont need to pass your variables because they are all global
;(i think) so you are changing the actual value of the variable (its like
; passing it by reference) so if you dont want that to happen use separate
;variables just in your functions. heres the rest of the function:
;initializing variables here
mov ax,left ; form: (move) (destination),(source)
sub ax,1 ;form: (sub) (destination),(source) outcome is dest=dest-source
mov lp,ax ; these three steps= lp = left - 1
mov ax,right ;you cant move memory to memory in assembly so it needs to go
mov rp,ax ;through a register first, like i did here
mov si,offset a ;this is how you get a pointer. it points to a in memory (si is a good pointer
add si,[right*2] ;register) now we add a + right * sizeof word (which is 2)
mov v,si ;si had a[right] in it and moved it to variable 'v'
if1: ;just to make it look better not neccissary here
mov ax,right ;i compare using a register and a memory i think you can do mem & mem tho
cmp ax,left ;this will set flags based on the outcome of the comparison
jle return ;this jumps if lessthan or equal, to return. sometimes you need jbe instead
mainloop: ;while(1) always returns true correct? so this is an endless loop
while2: ;ill name the while loops to make it easier to comprehend
inc lp ;increase lp by 1 (++lp)
mov si,offset a
add si,[lp*2] ;a[++lp]
cmp si,v ; < v
jge return ;if a[++lp]<v we want to continue otherwise we will jump to the end
while3:
dec rp ;--rp we are on the next while loop now
mov si,offset a
add si,[rp*2] ;a[--rp]
cmp v,si
jge while2 ;if v>a[--rp] than skip this part here and return to while 2, otherwise do it
if2:
mov ax,rp
cmp ax,left ;see if rp==left
je endofwhile ;this will simulate a break statement and will jump to the end of the while
;statements (i think this is how you want it)
if3: ;fall through to if statment number 3
mov ax,lp
cmp ax,rp ;if ax>= rp then 'break'
jge endofwhile
mov si,offset a
add si,[lp*2]
mov x,si ;you have to set up the variables before you call the function
mov si,offset a
add si,[rp*2]
mov y,si ;now the proc is ready to be called because x has a[lp] and y has a[rp]
call swap ;if all that is good we will call swap. notice to use a procedure you type
; (call) (procedure name)
jmp mainloop ;jump to main loop (while) if everything was ok
endofwhile
mov si,offset a
add si,[lp*2]
mov x,si ;you have to set up the variables before you call the function
mov si,offset a
add si,[right*2]
call swap
;when you first call quicksort you have to set the variables up in this fashion:
mov ax,left
mov left,ax ;sets up second parameter
dec lp ;lp-1
mov ax,lp
mov right, ax ;third parameter is set up now, so we can call the proc now
;we dont need to set up the first parameter because that would be pointless
;to set an array to an array just copy your array to the 'a' array if you have to
call quicksort
mov ax,right
mov right,ax ;set up 3rd parameter
inc lp ;lp+1
mov ax,lp
mov left,ax ;set up and ready to go
call quicksort
endofwhile:
return: ;point of returning to the program
ret ; this pops the cs:ip off the stack to return to the caller
endp ;says this is end of procedure (function)
;next procedure here----------------------
swap proc
mov ax,[x] ;move to ax whats pointed at by x
mov temp,ax ;temp = x
mov bx,[y]
mov [x],bx ;move y to what x is pointing to (im not sure if you need to brackets)
mov ax,temp
mov [y],ax ;move temp to what y points at
ret
endp ;end of swap procedure
i hope this works good for you, you might need a few minor adjustments tho. your code was a little confusing and i think you might have made a few mistakes in it, but that might be just me. i wasnt really sure where your while statments were SUPPOSED to end so i made a guess of what i thought was supposed to happen there.
if it doesnt work comment your C++ code more and tell me what the assembler errors were so i can fix it hopefully :)
one thing is for sure, this is a difficult way to do things and you should only do it if its absolutly neccissary ofr speed or otherwise.
Main Topics
Browse All Topics





by: grg99Posted on 2004-09-09 at 14:03:12ID: 12021428
A question: Why do you want to do this? It's hard and the benefits are few.
Most compiler nowdays can generate very good code, especially for this kind of simple stuff, just some if()s and calls. In assembly you'd be doing pretty much the same thing. There's no advantage to the usual trick of keeping values in registers (because of the recursion, you have to push/pop all of them anyway), or unrolling complex loops (there arent any), or using SIMD instructions (not much data to process in parallel here).
One sure way is to use a C compiler and capture its output (-S option IIRC).