Click to See Complete Forum and Search --> : will you help me


hilleanne
March 2nd, 2009, 10:39 AM
im new here and im a student. will anyone help me in displaying the proper output in my code?
my program must calculate the cube of a number and display their product.
i have problem in displaying the product of the cube it displays random results.
here is my code please help me.

.model small
.stack 100h
.data
msg1 db 0Dh,0Ah,"Enter number between 1-9: $"
msg2 db 0Dh,0Ah,"The cube of the number is: $"
.code
start:
mov ax,@data
mov ds,ax
mov dx,offset msg1
mov ah,09h
int 21h

mov ah,1h
int 21h

cube:
mov bx,ax
mul bx
mul bx
mov cx,ax
mov bx,ax

shr bl,4
add bl,30h

shl cl,4
shr cl,4
mov cl,30h

shr bh,4
add bh,30h

mov ch,30h

mov ax,@data
mov ds,ax
mov dx,offset msg2
mov ah,09h
int 21h

mov ah,2d
mov dl,ch
mov ah,02h
int 21h

mov ah,2d
mov dl,bh
mov ah,02h
int 21h

mov ah,2d
mov dl,cl
mov ah,02h
int 21h

mov ah,2d
mov dl,bl
mov ah,02h
int 21h

quit:
mov ax,4c00h
int 21h
end


please help me im begging you.
reply in my yahoo account.
krazi_krayne@yahoo.com

thank you to the one who may help me im very grateful.

rxbagain
March 2nd, 2009, 11:57 AM
Here's the code. See the comments and explanations.model small
.stack 100h
.data
msg1 db 0Dh,0Ah,"Enter number between 1-9: $"
msg2 db 0Dh,0Ah,"The cube of the number is: $"
.code
start:
mov ax,@data
mov ds,ax
mov dx,offset msg1
mov ah,09h
int 21h

ask_input:
mov ah,07h ; get the input without echo
int 21h
cmp al, '0' ; if input < '0' then ask again
jl ask_input
cmp al, '9' ; if input > '9' then ask again
jg ask_input
mov ah, 02h ; echo the valid input
mov dl, al
int 21h
sub al, '0' ; convert from ascii to number
xor ah, ah ; clear the upper byte

mov bx, ax ; do the multiplication
mul bx
mul bx

; since we have to show some message first before showing the number, we need to preserve the cube value we got
; we can use the stack to temporarily save the value of AX and pop it when we are ready to show it
push ax ; temporarily save ax (cube result)
mov dx,offset msg2
mov ah,09h
int 21h
pop ax ; retrive the saved cube value


; we are now ready to print the value of AX. we can't simply show the value of AX since it is in binary format
; we need to convert it first to string before we can show it to the user
; the algo is that we divide AX with 10 until AX becomes 0. Along the way, we get the remainder (modulo) and store it
; we cannot show the remainders immediately because if we do that, we will get string in reverse format.
; for this purpose, we use the stack to store the modulo values first and when becomes 0, we then show the modulo values
; in correct arrangement.

mov bx, sp ; we use bx as the END marker of our modulo values
divide_by10:
xor dx, dx ; clear dx first because we are doing 16bit divison (DX:AX / CX)
mov cx, 10 ; put divisor 10 in CX
div cx ; divide (quotient will go to AX, remainder to DX)
push dx ; we temporarily save the remainder (to the stack)
or ax, ax ; check if AX <> 0
jnz divide_by10 ; and divide again when AX <> 0

; after the module results are finished, the numbers are now in the stack.
; we now print them starting from the bottom of the stack

print_result:
mov ah, 2 ; use write character to standard output (int 21h function 2)
pop dx ; get 1 value (the modulo result) out of the stack
add dl, '0' ; add '0' to make it ascii for number
int 21h ; output it
cmp sp, bx ; have we reached the marker that we set before (BX)?
jne print_result ; if not yet, the we pop and print another

quit:
mov ax,4c00h
int 21h

end
Hope it will help you :)