Search code examples
assemblyx86-16emu8086

When i include the "The largest no. is:$' as MSG in data segment, i don't get the result


enter image description here

msg db 'The largest no is:$'

lea dx, msg
mov ah, 9
int 21h

if i don't include those lines, i get 9 as the largest which is correct. but i if include that prompt display, i get "a" as output. why? (if i don't write something, it doesn't let me upload so. ....................................................................................................................................................)

.model small
.stack 100h
.data
array db 1,0,2,3,4,5,6,7,9,8
msg db 'The largest no is:$'
largest db ?
.code
main proc
mov ax, @data
mov ds, ax

lea si, array
mov cx, 10
mov al, [si]
findlargest:cmp al, [si+1]
jnc skip
mov al, [si+1]
skip:inc si
loop findlargest

mov largest, al

mov ah, 0     ;;; clear the screen
mov al, 2
int 10h

mov dl, 13                      ;;; new line
mov ah, 2
int 21h
mov dl, 10
mov ah, 2
int 21h

lea dx, msg
mov ah, 9
int 21h

mov dl, largest
add dl, 48             ;; convert number to character to print
mov ah, 2
int 21h

mov ah, 4ch     ;; exit
int 21h
main endp
end


Solution

  • Your code is overflowing the byte array.

    First you load al via mov al, [si] to hold a starting value.

    Then you loop ten iterations referencing[si+1], which means you compare al against array+1 through array+11. And array+11 holds the character 'T', and is captured as the highest, so then when you add 48 to 'T' you get a funny character.

    If you omit the msg then array+11 most likely holds zero, so that won't be taken as the highest.