Had troubles coding in PWB. Multiplication.

Started by eaOn, October 02, 2008, 04:22:03 AM

Previous topic - Next topic

eaOn

Hi! I'm new to this masm and had problems with my 2-digit arithmetic operation [in multilplication]. I can't seem to figure how to create a temporary contaniner that will store my first interger and my second integer. For example, If I input 23 in my first integer and the second is 1. Then multiply them. The trouble is I can't store this 2 variable.

Here's my source code. Its a bit messy right now.
.model small
.586
.stack 100h
.data
buf1 db 5 dup (?)
buf2 db 5 dup (?)
;buf3 db 4 dup (?)
message1 db 0ah,0dh,"Enter your first integer: ",0ah,0dh,"$"
message2 db 0ah,0dh,"Enter your second integer: ",0ah,0dh,"$"
message3 db 0ah,0dh,"The answer is: ",0ah,0dh,"$"
.code
main proc
mov ax,@data
mov ds,ax

mov dx,offset message1 ;prompt for first input
mov ah,9
int 21h

mov buf1,3 ;limit input to 2 char
mov dx,offset buf1
call line

mov bl,dl

mov dx,offset message2 ;prompt for second input
mov ah,9
int 21h


mov buf2,3 ;limit input to 2 char
mov dx,offset buf2
call line

;mov cl,dl

jmp outsideLine

;mov buf2,2
;mov dx,offset buf2
;call line


line proc near
mov ah,0ah
int 21h
ret
line endp

outsideLine:

mov dx,offset message3
mov ah,9
int 21h

;mov al,buf1
;mov cl,buf2
mul cl
;int 3
;int 3

mov ah,1
int 21h

mov ax,4c00h
int 21h



main endp
end main


I'm not sure if I delivered  my buf1 and buf2 but I'm pretty sure it should store them. I've check it and debug it but it's not working. What am I missing? Little help please. ;(

Suggestion or links is greatly appreciated.  :red

FORTRANS

Hi,

   Well, you are asking for user input, and that will be in the form
of ASCII characters.  You need to convert that text into a binary
number before you can do anything with it.  You also need to see
where that input is within your buffers.  For function 0AH, the first
byte will be the length of the buffer, the second byte shows the
length of the input, and the third byte has the start of the input.

HTH,

Steve N.

eaOn

Quote from: FORTRANS on October 02, 2008, 02:14:08 PM
Hi,

   Well, you are asking for user input, and that will be in the form
of ASCII characters.  You need to convert that text into a binary
number before you can do anything with it.  You also need to see
where that input is within your buffers.  For function 0AH, the first
byte will be the length of the buffer, the second byte shows the
length of the input, and the third byte has the start of the input.

HTH,

Steve N.
Thanks for replying. I somehow manage to figure it out most of it myself but there is still a bug when calling 08h [ASCII "backspace"].

.model small
.586
.stack 100h
.data
msg1 db 0ah,0dh,0ah,"Enter first integer: ",0ah,0dh,"$"
msg2 db 0ah,0dh,0ah,"Enter second integer: ",0ah,0dh,"$"
msg3 db 0ah,0dh,0ah,"The answer is: ",0ah,0dh,"$"


buf1 dw ?
buf2 dw ?

buf3 dw ?

buf4 dw ?


.code
main proc
mov ax,@data
mov ds,ax
start:
mov dx,offset msg1
mov ah,9
int 21h

mov cx,2

readkey1:
mov ah,6
cmp al,08h
je backspace
cmp al,30h
jb filterkey1
cmp al,39h
ja filterkey1
jmp displaykey1


backspace:
mov dl,08h
int 21h
mov ax,0
mov dx,0
jmp start


filterkey1:
mov dl,0ffh
int 21h
jmp readkey1

displaykey1:
mov dl,al
mov ah,2
int 21h

cmp cx,2
je secondDigit1


cmp cx,1
je firstDigit1


secondDigit1:
mov ax,0
mov al,dl
sub al,30h
mov dx,0ah
mul dx
mov buf1,ax
jmp next1

firstDigit1:
mov ax,0
mov al,dl
sub al,30h
mov dx,1h
mul dx
add buf1,ax

mov al,0

next1:
loop readkey1
begin:
mov dx,offset msg2
mov ah,9
int 21h

mov cx,2

readkey2:
mov ah,6
cmp al,08h
je delete
cmp al,30h
jb filterkey2
cmp al,39h
ja filterkey2
jmp displaykey2

delete:
mov dl,08h
int 21h
mov ax,0
mov dx,0
jmp begin



filterkey2:
mov dl,0ffh
int 21h
jmp readkey2

displaykey2:
mov dl,al
mov ah,2
int 21h

cmp cx,2
je secondDigit2

cmp cx,1
je firstDigit2

secondDigit2:
mov ax,0
mov al,dl
sub al,30h
mov dx,0ah
mul dx
add buf2,ax
jmp next2

firstDigit2:
mov ax,0
mov al,dl
sub al,30h
mov dx,1h
mul dx
add buf2,ax

next2:
loop readkey2

mov dx,offset msg3
mov ah,9
int 21h

mov ax,0
mov ax,buf1
mov bx,0
mov bx,buf2
mul bx

lastDigit:
mov buf3,ax
mov bx,03e8h
mov dx,0
div bx
mov buf4,dx
add al,30h
mov dl,al
mov ah,2
int 21h

thirdDigit:
mov ax,buf4
mov bx,64h
mov dx,0
div bx
mov buf4,dx
add al,30h
mov dl,al
mov ah,2
int 21h

secondDigit:
mov ax,buf4
mov bx,0ah
mov dx,0
div bx
mov buf4,dx
add al,30h
mov dl,al
mov ah,2
int 21h

firstDigit:
mov ax,buf4
mov bx,1h
mov dx,0
div bx
add al,30h
mov dl,al
mov ah,2
int 21h

mov ax,4c00h
int 21h


main endp
end main


What do you think? Any suggestions?  :P

FORTRANS

Well this is a very quick look (on a different machine).


readkey1:
mov ah,6
cmp al,08h
je backspace


It looks like you forgot the INT 21H.

Steve

eaOn

Quote from: FORTRANS on October 08, 2008, 04:47:30 PM
Well this is a very quick look (on a different machine).


readkey1:
mov ah,6
cmp al,08h
je backspace


It looks like you forgot the INT 21H.

Steve
Int 21h is not needed. It's only a condition if to jump or not:
If 08h [Ascii for "backspace"] is in stored in ax.
Jump on backspace.
Else don't jump.

MichaelW

It seems to work OK, but your code is hard to follow. One of the problems is that you load the function number into AH in one place, and load the other registers and call the interrupt from another place, requiring that the reader trace through the code to know which function is being called.

If you encapsulated your character to number value conversion code, or better, 2 character to number value conversion code, in a procedure, and replaced the separate conversions with a call to that procedure, your code would be smaller and easier to understand.

If instead of getting your input 1 character at a time, if you used the Read File or Device function (3Fh) specifying the system-supplied handle STDIN (= 0), you could get the entire number in one function call, and the function would handle backspace internally.
eschew obfuscation

eaOn

Quote from: MichaelW on October 15, 2008, 05:04:52 PM
It seems to work OK, but your code is hard to follow. One of the problems is that you load the function number into AH in one place, and load the other registers and call the interrupt from another place, requiring that the reader trace through the code to know which function is being called.

If you encapsulated your character to number value conversion code, or better, 2 character to number value conversion code, in a procedure, and replaced the separate conversions with a call to that procedure, your code would be smaller and easier to understand.

If instead of getting your input 1 character at a time, if you used the Read File or Device function (3Fh) specifying the system-supplied handle STDIN (= 0), you could get the entire number in one function call, and the function would handle backspace internally.

It works alright, got it running. It run slow on my system though and you bet it reads like spaghetti.  :lol
It's one of my project that had me worn out. Might as well port this out in masm in the future.  :U