Find the two largest numbers passed on the stack and multiply them, return DX:AX pair - sorting

I have an assignment where we are passed 4 values on the stack (v1, v2, v3, v4), are to find the two largest values out of the four, and then multiply them together to return the DX:AX pair.
This is the code that I have come up with so far, comparing all the values to one another and storing the highest value in AX and the second highest value in BX. The problem is that the code hangs when tested in DOSbox, and I'm not sure what is causing it.
EDIT: Finished and working!
;---------------------------------------
;
; Code Segment
;
;---------------------------------------
_linkhll:
push bp ; saves the caller's bp
mov bp,sp ; loads bp from sp
MOV AX,[bp+4] ;Load v1 to AX
MOV BX,[bp+6] ;Load v2 to BX
;---------------------------------------
; Find the first largest number
;---------------------------------------
CMP AX,BX ;compare value 1 and 2
JE doubles
CMP AX,BX
JA L2 ;AX > BX, goto L2
MOV AX,BX ;make v2 the largest number
L2:MOV BX,[bp+8] ;Load v3 to BX
CMP AX,BX ;compare value AX and v3
JE doubles
CMP AX,BX
JA L3 ;AX > BX, goto L3
MOV AX,BX ;make v3 the largest number
L3:MOV BX,[bp+10] ;Load v3 to BX
CMP AX,BX ;compare value AX and v4
JE doubles
JA S1 ;AX > BX, goto L3
MOV AX,BX ;make v4 the largest number
JMP s1
doubles:
MOV CX,[bp+8] ;mov v3 to cx
CMP AX,CX ; BX > CX
JA v4bigger ; yes, skip to v4 test
MOV AX,CX ;if no, make CX the new AX
v4bigger:
MOV CX,[bp+10] ;v4 to CX
CMP BX,CX ;Compare to current highest
JA mult
MOV BX,CX
JMP mult
;---------------------------------------
; Find the second largest number
;---------------------------------------
S1:MOV BX,[bp+4] ;Load v1 to BX
MOV CX,[bp+6] ;load v2 to CX
CMP AX,BX ;compare value AX and v1
JE v2mov ;AX = BX, multiply them
CMP AX,CX ;compare value AX and v2
JE s2 ;AX = CX, mov cx to bx and multiply
CMP BX,CX ;Compare v1 and v2
JA S2 ;BX > CX goto S2
v2mov:
MOV BX,CX ; make v2 the current second highest
S2:MOV CX,[bp+8] ;load v3 to CX
CMP AX,CX ;compare value AX and v3
JE s3 ;AX = CX, goto S3
CMP BX,CX ;Compare AX and v3
JA S3 ;BX > CX goto S3
v3mov:
MOV BX,CX ;mov v3 to 2nd highest number spot
S3:MOV CX,[bp+10] ;load v4 to CX
CMP AX,CX ;compare value AX and v4
JE mult ;AX = CX, goto S3 // mult????
CMP BX,CX ;Compare AX and v3
JA mult ;BX > CX goto S3
v4mov:
MOV BX,CX ;Make v4 second highest number
mult:
MUL BX ;multiply ax by bx
POP BP
;
;
;
ret ;
;
end ;
;---------------------------------------

You're pushing bp on the stack at the start of your function. But you don't pop it off the stack before returning. Hence, when the ret instruction executes and tries to get the return address off the stack it gets the old value of bp instead.

Related

Incorrect value of the variable ans that stores the LCM of two numbers (8086 Program)

Following is the code I wrote to find LCM of two numbers in EMU8086. When I ran it, I am getting value 0 in the Ans variable.
.MODEL SMALL
.DATA
Num1 DW 250
Num2 DW 100
Ans DW ?
.CODE
MOV AX,#DATA
MOV DS, AX
MOV AX, Num1
MOV BX, Num2
MOV DX, 0000h
NEXT: PUSH AX
PUSH DX
DIV BX
CMP DX, 0000h
JZ LAST
POP DX
POP AX
ADD AX, Num1
JNC NEXT
INC DX
JMP NEXT
LAST: POP Ans+2
POP Ans
MOV AH, 4Ch
INT 21h
END
LCM(a, b) = a * b / GCD(a, b)
Due to this equation, you can find GCD using Euclid's algorithm and then calculate LCM. Assuming numbers a and b are in al and dl, this code calculate LCM.
; Save multiplication value
MOV AL, DL ; This 2 lines is AL * DH
MUL DH
PUSH AX ; Store result in stack
FINDBCD: ; General idea is : LCM(a, b) = a*b/BCD(a,b)
; We calculate BCD with euclidean algorithm
CMP DH, DL
JNS CALCULATE ; Jump if DL < DH else swap them
MOV CL, DL ; This 3 line swap DL and DH
MOV DL, DH
MOV DH, CL
CALCULATE:
MOV AL, DH ; Move greater number in AL
XOR AH, AH ; Zero AX 8 second bits
DIV DL ; This is AL/DL
CMP AH, 0 ; If remainder is zero so we found BCD
JE AFTERFINDBCD
SUB DH, DL ; Else substract smaller number from greater number
JMP FINDBCD ; Do this until find BCD
AFTERFINDBCD:
CMP DH, DL
JNS FINDLCM ; Jump if DL < DH
MOV CL, DL ; This 3 line swap DL and DH
MOV DL, DH
MOV DH, CL
FINDLCM:
POP AX ; Retreive multiplication result
DIV DL ; This is AX/DL
AND AX, 0000000011111111B ; Ignore remainder

I want to make a bubble sort in assembly, and I don't understand why it's not working

I have a function "swapByRef" that works in this code (this code just checks if the function swaps the values).
MODEL small
STACK 10h
DATA SEGMENT
a dw 12h
b dw 0A9h
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
start:
mov ax, DATA
mov ds, ax
push offset a ;push to the stack the adress of the variable a
push offset b ;push to the stack the adress of the variable b
call swapByRef
exit:
mov ax, 4c00h
int 21h
swapByRef proc
mov bp, sp
mov bx, [bp + 2]
mov ax, [bx]
mov si, [bp + 4]
mov cx, [si]
mov [bx], cx
mov[si], ax
ret 4
swapByRef endP
CODE ENDS
END start
But in my code (the bubble sort code) the procedure doesn't swap the values in the array and the array does not get sorted.
MODEL small
STACK 100h
DATA SEGMENT
ARR dw 9,5,7,3,8
len dw 5
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
start:
mov ax, DATA
mov ds, ax
mov bx, offset ARR
sorting:
mov ax, len
dec ax
cmp bx, ax
je redo
mov ax, ARR[bx]
mov dx, ARR[bx+ 2]
cmp al, ah
jg swap
jmp continue
swap:
push offset [ARR + bx]
push offset [ARR + bx + 2]
call swapByRef
continue:
inc bx
jmp sorting
redo:
cmp len, 0
je exit
mov ax, len
dec ax
mov len, ax
xor bl, bl
jmp sorting
exit:
mov ax, 4c00h
int 21h
swapByRef proc
mov bp, sp
mov bx, [bp + 2]
mov ax, [bx]
mov si, [bp + 4]
mov cx, [si]
mov [bx], cx
mov[si], ax
ret 4
swapByRef endP
CODE ENDS
END start
I've tried to debug and still I couldn't find the problem in my code...
Any help will be awesome, thanks.
mov bx, offset ARR
...
mov ax, ARR[bx]
mov dx, ARR[bx+ 2]
You're adding the offset to the array twice! You need to initialize BX=0.
mov ax, ARR[bx]
mov dx, ARR[bx+ 2]
cmp al, ah
jg swap
jmp continue
swap:
You've read the elements in AX and DX. Then also compare AX and DX.
You can also write it shorter like this:
mov ax, ARR[bx]
cmp ax, ARR[bx+2]
jng continue
swap:
Given that the array contains words and that BX is an offset within the array, you need to change BX in steps of 2. Write add bx, 2 instead of inc bx.
This also means that it's best to set len dw 10 and modify it in steps of 2 using sub word ptr len, 2.
swapByRef proc
mov bp, sp
mov bx, [bp + 2]
mov ax, [bx]
mov si, [bp + 4]
mov cx, [si]
mov [bx], cx
mov[si], ax
ret 4
Your swapByRef proc destroys a lot of registers. Especially loosing BX is problematic!
This is a general solution to not clobber registers. Optimize as needed.
swapByRef proc
push bp
mov bp, sp
push ax
push bx
push cx
push si
mov bx, [bp + 4]
mov ax, [bx]
mov si, [bp + 6]
mov cx, [si]
mov [bx], cx
mov [si], ax
pop si
pop cx
pop bx
pop ax
pop bp
ret 4

Sorting a list of ten numbers with selection sort in assembly language

sorting a list of ten numbers with selection sort in assembly language.
How does i convert this bubble sort method into selection sort method
`[org 0x0100]
jmp start
data: dw 60, 55, 45, 50, 40, 35, 25, 30, 10, 0
swap: db 0
start: mov bx, 0 ; initialize array index to zero
mov byte [swap], 0 ; rest swap flag to no swaps
loop1: mov ax, [data+bx] ; load number in ax
cmp ax, [data+bx+2] ; compare with next number
jbe noswap ; no swap if already in order
mov dx, [data+bx+2] ; load second element in dx
mov [data+bx+2], ax ; store first number in second
mov [data+bx], dx ; store second number in first
mov byte [swap], 1 ; flag that a swap has been done
noswap: add bx, 2 ; advance bx to next index
cmp bx, 18 ; are we at last index
jne loop1 ; if not compare next two
cmp byte [swap], 1 ; check if a swap has been done
je start ; if yes make another pass
mov ax, 0x4c00 ; terminate program
int 0x21`
The key here is to change your loop. Currently it's swapping adjacent numbers. You need to change it to copy the rightmost element into a register, and shift the pre-existing sorted array to the right until the element you just shifted is greater than or equal to the previously rightmost element you just copied into your register.
Maybe this will be helpfull. I wrote this a long time ago. Realmode intel assembler.
MAIN.ASM
SSTACK SEGMENT PARA STACK 'STACK'
DW 128 DUP(?)
SSTACK ENDS
DSEG SEGMENT PUBLIC 'DATA'
S LABEL BYTE
ARR DB 'IHGFED27182392JASKD1O12312345CBA'
LEN EQU ($-S)
PUBLIC TMP
PUBLIC MIN
TMP DW ?
MIN DW ?
DSEG ENDS
CSEG SEGMENT 'CODE'
ASSUME CS:CSEG, SS:SSTACK, DS:DSEG
EXTRN OUTPUT:NEAR
EXTRN SORT:NEAR
START: MOV AX, DSEG
MOV DS, AX
MOV BX, OFFSET ARR
MOV CX, LEN
CALL OUTPUT
MOV AX, 60
CMP AX, 0
JZ NO_SORT
CMP AX, 1
JZ NO_SORT
MOV BX, OFFSET ARR
MOV CX, LEN
CALL SORT
NO_SORT: MOV BX, OFFSET ARR
MOV CX, LEN
CALL OUTPUT
MOV AH, 4CH
MOV AL, 0
INT 21H
CSEG ENDS
END START
SORT.ASM
DSEG SEGMENT PUBLIC 'DATA'
EXTRN TMP:WORD
EXTRN MIN:WORD
DSEG ENDS
CSEG SEGMENT 'CODE'
ASSUME CS:CSEG, DS:DSEG
PUBLIC SORT
SORT PROC; (AX - N, BX - ARRAY ADDRESS, CX - ARRAY LENGTH)
PUSH SI
PUSH DI
PUSH DX
CALL COMPARE_MIN
DEC CX
MOV AX, CX
XOR SI, SI
XOR DI, DI
L1:
PUSH CX
MOV MIN, SI
MOV TMP, DI
INC DI
MOV CX, AX
L2:
MOV DH, BYTE PTR[BX+DI]
PUSH SI
MOV SI, MIN
MOV DL, BYTE PTR[BX+SI]
POP SI
CMP DH, DL
JA OLD_MIN
NEW_MIN: MOV MIN, DI
OLD_MIN: INC DI
DEC CX
CMP CX, TMP
JNZ L2
SWAP: PUSH DI
MOV DI, MIN
MOV DL, BYTE PTR[BX+DI]
MOV DH, BYTE PTR[BX+SI]
MOV BYTE PTR [BX+SI], DL
MOV BYTE PTR [BX+DI], DH
POP DI
INC SI
MOV DI, SI
POP CX
LOOP L1
POP DX
POP DI
POP DI
RET
SORT ENDP
COMPARE_MIN PROC; (AX - A, CX - B CX - MIN)
PUSH AX
CMP AX, CX
JB B__A
JA A__B
A__B: MOV CX, CX
JMP EX
B__A: MOV CX, AX
JMP EX
EX: POP AX
RET
COMPARE_MIN ENDP
CSEG ENDS
END
OUTPUT.ASM
CSEG SEGMENT 'CODE'
ASSUME CS:CSEG
PUBLIC OUTPUT
OUTPUT PROC ; (BX - ARRAY ADDRESS, CX - ARRAY LENGTH)
PUSH DX
PUSH SI
PUSH AX
XOR SI, SI
MOV AH, 02H
OUTARR: MOV DL,[BX+SI]
INT 21H
INC SI
LOOP OUTARR
MOV DL, 10
INT 21H
POP AX
POP SI
POP DX
RET
OUTPUT ENDP
CSEG ENDS
END

Assembly(tasm) program crashes in run but works fine in debugger(turbo debugger)

My program(assembly tasm 16bit) supposed to print a bar graph represtion for an array.Right now its only supports specificarray but I will add support for general cases in the future.The code works fine in debugger and prints the bars as expcted.But in run the code gets stuck and dosent print anything. All the functions apart from PrintArr were working separately as expcted. I cant find my problam in debugging beacouse the problame dosent seem to exist in the debugger.
;
IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
; Your variables here
; --------------------------
arr db 3,1,2
screen_width dw 300
screen_height dw 190
plo dw 0
var db ?
CODESEG
;works on tasm syntex 16 bit
proc FindWidthForColAndSpace
;finds the width for each col and space
;input:
;1.number of cols
;2.screen width
;ouput:
;1.space
;2.width
push bp
mov bp,sp
push ax
push bx
push cx
push dx
mov ax,[bp+4];sceen width
mov bx,[bp+6];number of cols
div bx
xor dx,dx
mov bx,ax
mov cx,5
mul cx
xor dx,dx
mov cx,100
div cx
xor dx,dx
sub bx,ax
mov [bp+4],ax
mov [bp+6],bx
pop dx
pop cx
pop bx
pop ax
pop bp
ret
endp FindWidthForColAndSpace
proc FindHeight
;finds the pixel repsention for
;input:
;1.screen height
;2.highest value
;3.lowest value
;ouput:
;1.height
push bp
mov bp,sp
push ax
push bx
push cx
push dx
xor dx,dx
mov cx,[bp+4];lowest value
mov bx,[bp+6];highest value
mov ax,[bp+8];screen height
div bx
mov [bp+8],ax
pop dx
pop cx
pop bx
pop ax
pop bp
ret 4
endp FindHeight
proc PrintLine
;prints a line
;1.length
;2.colour
;3.x
;4.y
push bp
mov bp,sp
push ax
push bx
push cx
push dx
push si
mov cx,[bp+10];leangth
mov dx,[bp+4];y
mov al,[bp+8];colour
mov si,[bp+6];x
mov ah,0ch
xor bx,bx
pl:
push cx
mov cx,si
int 10h
inc si
pop cx
loop pl
pop si
pop dx
pop cx
pop bx
pop ax
pop bp
ret 8
endp PrintLine
;clean screen
proc Cls
push ax
push cx
mov cx,200
xor ax,ax
Clean:
push 320
push 0
push 0
push ax
call PrintLine
inc ax
loop Clean
pop cx
pop ax
ret
endp cls
proc PrintSquare
;print a square
;input:
;1.height
;2.leangth
;3.colour
;4.x
;5.y
push bp
mov bp,sp
push ax
push bx
push cx
push dx
push di
push si
mov cx,[bp+12]
mov ax,[bp+10]
mov bx,[bp+8]
mov dx,[bp+6]
mov di,[bp+4]
xor si,si
print:
mov di,[bp+4]
push ax
push bx
push dx
sub di,si
push di
call PrintLine
inc si
loop print
pop si
pop di
pop dx
pop cx
pop bx
pop ax
pop bp
ret 10
endp PrintSquare
proc PrintArr
;prints a array
;1.strat of the array(offset)
;2.end of the array (offset)
;output
;none
push bp
mov bp,sp
push ax
push bx
push cx
push dx
push di
push si
mov bx,[bp+6];strat of the array(offset)
mov ax,[bp+4];end of the array (offset)
mov cx,[screen_width]
push 3
push cx
call FindWidthForColAndSpace
pop dx;space widfth
pop di;cooloum width
mov cx,[screen_height]
push cx
push 3
push 1
call FindHeight
pop si;height(dyamnic height *value =pixels)
mov cx,3
xor ax,ax
printar:
xor ax,ax
mov al,[byte ptr bx]
push dx
xor dx,dx
mul si
pop dx
push ax
push di
push 4
push [plo]
push [screen_height]
call PrintSquare
mov ah,1
int 21h
inc bx
push ax
mov ax,[plo]
add ax,dx
add ax,di
mov [plo],ax
pop ax
loop printar
pop si
pop di
pop dx
pop cx
pop bx
pop ax
pop bp
ret 4
endp PrintArr
start:
mov ax, #data
mov ds, ax
mov ax,13h
int 10h
call cls
push 0
push 2
call PrintArr
mov ah,1
int 21h
; push 10
; push 5
; push 4
; push 100
; push 100
; call PrintSquare
; mov ah,86h
; int 15h
;call cls
exit:
mov ax, 4c00h
int 21h
END start
Turbo Debugger sets a bunch of registers to 0 when it loads the program. When MS-DOS starts these registers are not set to null. The information which register needs to be null you can achieve by adding
xor ax, ax
xor bx, bx
xor cx, cx
xor dx, dx
xor si, si
xor di, di
xor bp, bp
at the beginning of the start procedure and by commenting it out successively. It will turn out that the delinquent is DX. So search for the first function or instruction which expects a null in this register. I found it in the first div instruction in FindWidthForColAndSpace. This div performs DX:AX/BX and needs therefore a value in DX. Is it an accident that the line xor dx, dx follows the div? It must be in front of it.

Assembly Procedures not running Intel 8086

The code below, is supposed to get 20 user-entered numbers (6 digit numbers or less) and compute the average as well as sorting them, When I set it to get 6 or less numbers, it works fine. But when it is set to get 7-20 numbers, after getting the numbers, it skips the next procedures and someimes runs the GetNum procedure ( The one that gets the numbers from user) again and when it gets 11 numbers, I get this message "PROGRAM HAS RETURNED CONTROL TO THE OPERATING SYSTEM".
ShowMsg macro msg
mov ah, 09h
mov dx, offset msg
int 21h
endm
NewLine macro
mov ah, 02h
mov dl, 0ah
int 21h
mov dl, 0dh
int 21h
endm
data segment
sum dd 0
num dd 0
ave dd 0
array dd 20 dup(0)
msg1 db 'Enter 20 numbers:', '$'
msg2 db 0dh,0ah,'Average: ', '$'
temp dd ?
data ends
stack segment
dd 100 dup(?)
stack ends
code segment
assume cs:code, ds:data, ss:stack
Main Proc Far
mov ax, data
mov ds, ax
mov ax, stack
mov ss, ax
;Printing first message.
ShowMsg msg1
call GetNum
call Average
call Sort
call Print
mov ah, 4ch
int 21h
Main endp
proc GetNum
mov bp, 0
mov ch, 20
NextNumber:
NewLine
mov cl, 6
mov word ptr num, 0
mov word ptr num+2, 0
GetChar:
mov ah, 07h
int 21h
cmp al, 0dh
jz Flag
cmp al, 30h
jb GetChar
cmp al, 39h
ja GetChar
mov ah, 02h
mov dl, al
int 21h
sub al, 30h
mov bl, al
mov di, 10
mov ax, num
mul di
mov num, ax
push dx
mov ax, num+2
mul di
mov num+2, ax
pop dx
add num+2, dx
mov bh, 0
add num, bx
adc word ptr num+2, 0
dec cl
jnz GetChar
Flag:
mov ax, num
mov dx, num+2
mov array[bp], ax
mov array[bp+2], dx
add bp, 4
add sum, ax
adc sum+2, dx
dec ch
jnz NextNumber
ret
GetNum endp
proc Average
mov bx, 20
mov dx, 0
mov ax, word ptr sum+2
div bx
mov word ptr ave+2, ax
mov ax, word ptr sum
div bx
mov word ptr ave, ax
ShowMsg msg2
mov cl, 0
Next1:
mov bx, 10
mov dx, 0
mov ax, word ptr ave+2
div bx
mov word ptr ave+2, ax
mov ax, word ptr ave
div bx
mov word ptr ave, ax
push dx
inc cl
cmp ave, 0
jnz Next1
Next2:
pop dx
add dl, 30h
mov ah, 02h
int 21h
dec cl
jnz Next2
NewLine
ret
Average endp
proc Sort
mov ch, 20
OuterFor:
mov bp, 0
Cmp1:
mov ax, array[bp+2]
mov bx, array[bp+6]
cmp ax,bx
ja Xchange
cmp ax,bx
jz Cmp2
jmp Next
Cmp2:
mov ax, array[bp]
mov bx, array[bp+4]
cmp ax, bx
ja Xchange
jmp Next
Xchange:
mov ax, array[bp]
mov dx, array[bp+2]
mov temp, ax
mov temp+2, dx
mov ax, array[bp+4]
mov dx, array[bp+6]
mov array[bp], ax
mov array[bp+2], dx
mov ax, temp
mov dx, temp+2
mov array[bp+4], ax
mov array[bp+6], dx
Next:
add bp, 4
cmp bp, 76
jnz Cmp1
dec ch
jnz OuterFor
ret
Sort endp
proc Print
mov bp, 0
C:
mov cl, 0
A:
mov bx, 10
mov dx, 0
mov ax, array[bp+2]
div bx
mov array[bp+2], ax
mov ax, array[bp]
div bx
mov array[bp], ax
push dx
inc cl
mov ax, array[bp]
mov dx, array[bp+2]
or ax, dx
jnz A
B:
pop dx
add dl, 30h
mov ah, 02h
int 21h
dec cl
jnz B
add bp, 4
NewLine
cmp bp, 80
jnz C
ret
Print endp
code ends
end main
The problem lies with these two lines (and possibly similar elsewhere):
mov array[bp], ax
mov array[bp+2], dx
By default, the bp register addresses the stack segment, not the data segment where array is. You must either use another index register, or over ride the segment with
mov ds:array[bp], ax
mov ds:array[bp+2], dx
If it worked with a small number of elements, that was by luck that nothing was corrupted to make a crash or spoil the data.
UPDATE
I would suggest modifying the GetNum proc so you can use bx to index array, instead of bp.
proc GetNum
mov bx, 0
mov ch, 20
NextNumber:
push bx
NewLine
...
pop bx
mov array[bx], ax
mov array[bx+2], dx
add bx, 4
...
Similarly with your sorting function - swap the roles of bx and bp. It it better to use bp as a general purpose register and bx as an indexing register.

Resources