.model small
.stack 64
.data
error_alpha db "Invalid input. Expression contains letter/s. ",13,10,"$"
error_range db "Invalid input. Operand/s may be out of range. ",13,10,"$"
error_overflow db "An overflow has occurred. ",13,10,"$"
error_invalidinput db "Invalid Input. ",13,10, "$"
redo db "Please enter a valid one.",13,10,"$"
newline db 13,10,"$"
inputlen dw 2
op1len dw 1
op2len dw 2
prompt db "Enter an expression: ","$"
inputLabel label byte
inputmaxLen db 50
inputcurLen db ?
inputData db 50 dup (?)
str_operand1 db 10 dup (?)
str_operand2 db 10 dup (?)
str_operator db 2 dup (?)

operand_sign1 db 2 dup (?)
operand_sign2 db 2 dup (?)

sign_counter db 0h
multbyTen db 10
op_temp1 dw ?
op_temp2 dw ?

op_fin1 dw ?
op_fin2 dw ?
tempAnswer dw ?
finalAnswer dw ?
divAnswer dw 0h
remainder db ?
finalRem dw ?
outputDecimal db "DECIMAL: $"
outputBinary db "BINARY: $"
outputOctal db "OCTAL: $"
outputHex db "HEXADECIMAL: $"

rev_ascii_decimal db ?
rev_ascii_binary db ?
rev_ascii_octal db ?
rev_ascii_hexadecimal db ?
outputSign db ?

dec_len dw 0h
bin_len dw 0h
oct_len dw 0h
hex_len dw 0h


find_operand1 dw 0h
find_operand2 dw 0h

temp dw 0h

sum dw 0h
multiplier dw 0h
product dw 0h
.code
;asks the user for input
prompt_for_input proc
mov dx, offset prompt
mov ah, 9h
int 21h
mov dx, offset inputLabel
mov ah, 0ah
int 21h
mov bx, 0h
mov bl, inputcurLen
mov inputData[bx], "$"
mov bx, 0
mov bl, inputcurLen
mov inputlen, bx
ret
prompt_for_input endp
; gets the operands and the operator from the one line of input
get_operators_and_operands proc
mov ax, 0
mov bx, 0
mov dx, 0
mov cx, 0
mov si, 0
mov bp, 0
mov op1len, 0h
mov op2len, 0h

pre_conv1:
mov al, inputData[bx]
call check_if_digit
jz pre_start_operand1

check_sign1:
cmp al, "-"
je negative_op1
cmp al, "+"
je positive_op1

call invalid_input

negative_op1:
mov operand_sign1[0], "-"
mov operand_sign1[1], "$"
inc bx
jmp start_operand1

positive_op1:
mov operand_sign1[0], "+"
mov operand_sign1[1], "$"
inc bx
jmp start_operand1

pre_start_operand1:
mov operand_sign1[0], "+"
mov operand_sign1[1], "$"
jmp start_operand1

start_operand1:
mov al, inputData[bx]
cmp al, " "
je end_operand1
push bx
mov bx, 0
mov bx, cx
mov str_operand1[bx],al
pop bx
inc op1len
inc cx
inc bx
jmp start_operand1

end_operand1:
push bx
mov bx, 0
mov bx, cx
mov str_operand1[bx], "$"
pop bx
jmp get_operator

get_operator:
inc bx
mov al, inputData[bx]
mov str_operator[0], al
mov str_operator[1], "$"
inc bx
inc bx
mov ax, 0
mov cx, 0
jmp pre_conv2

pre_conv2:
mov al, inputData[bx]
call check_if_digit
jz pre_start_operand2
jnz check_sign2

check_sign2:
cmp al, "-"
je negative_op2
cmp al, "+"
je positive_op2

call invalid_input

pre_start_operand2:
mov operand_sign2[0], "+"
mov operand_sign2[1], "$"
jmp start_operand2

start_operand2:
mov al, inputData[bx]
cmp al, "$"
je end_operand2
push bx
mov bx, 0
mov bx, cx
mov str_operand2[bx],al
pop bx
inc op2len
inc cx
inc bx
jmp start_operand2

end_operand2:
push bx
mov bx, 0
mov bx, cx
mov str_operand2[bx], "$"
pop bx

ret

negative_op2:
mov operand_sign2[0], "-"
mov operand_sign2[1], "$"
inc bx
jmp start_operand2

positive_op2:
mov operand_sign2[0], "+"
mov operand_sign2[1], "$"
inc bx
jmp start_operand2
get_operators_and_operands endp
;checks what operator was used and performs the indicated operation
check_operator proc
mov bx, 0
mov bl, str_operator[0]
cmp bl, "+"
jne check_sub
call add_nos
ret
check_sub:
cmp bl, "-"
jne check_mul
call sub_nos
ret
check_mul:
cmp bl, "*"
jne check_div
call mul_nos
ret
check_div:
cmp bl, "/"
jne call_invalid_input
call div_nos
ret
call_invalid_input:
call invalid_input
check_operator endp
;jumps to the procedure input_out_of_range
to_input_out_of_range proc
jmp input_out_of_range
to_input_out_of_range endp
;converts the operands to decimal to be used in the arithmetic operations
convert_operands_to_decimal proc
mov bx, 0h
find_eostring1:
mov ax, 0
mov al, str_operand1[bx]
cmp al, "$"
je two
sub al, 30h
mov str_operand1[bx], al
inc bx
jmp find_eostring1
two:
mov bx, 0h
find_eostring2:
mov ax, 0
mov al, str_operand2[bx]
cmp al, "$"
je exit_cotd
sub al, 30h
mov str_operand2[bx], al
inc bx
jmp find_eostring2

exit_cotd:
cmp op1len, 6
jg to_input_out_of_range2
cmp op2len, 6
jg to_input_out_of_range2

ret
convert_operands_to_decimal endp
;performs addition if the operator is the '+' sign
add_nos proc
mov cl, operand_sign1[0]
mov bl, operand_sign2[0]
cmp cl, bl
jne subit
mov al, str_operand1
mov dl, str_operand2
add al, dl
add al, 30h
mov dl, al
mov ah, 9h
int 21h
ret
subit:
mov bx, op1len
mov cx, op2len
cmp bx, cx
jl subop2
subop1:
mov cl, operand_sign1[0]
mov outputSign[0],cl
mov outputSign[1],"$"
sub al, dl
ret
subop2:
mov dl, operand_sign2[0]
mov outputSign[0],dl
mov outputSign[1],"$"
sub dl, al
ret
add_nos endp
;performs subtraction if the operator is the '-' sign
sub_nos proc
ret
sub_nos endp
;jumps to the procedure input_out_of_range
to_input_out_of_range2 proc
jmp input_out_of_range
to_input_out_of_range2 endp
;performs multiplication if the operator is the '*' sign
mul_nos proc
mov al, str_operand1
mov dl, str_operand2
mul dl
mov finalAnswer,ax

mov cl, operand_sign1[0]
mov bl, operand_sign2[0]
cmp cl, bl
je outputSign_is_pos
mov outputSign[0],"-"
jmp endsignmul
outputSign_is_pos:
mov outputSign[0]," "
endsignmul:
mov outputSign[1],"$"
ret
mul_nos endp
;performs division if the operator is the '/' sign
div_nos proc
mov dx, 0
mov al, str_operand1
mov bl, str_operand2
div bl
mov remainder, ah
mov ah, 0
mov divAnswer, ax

mov cl, operand_sign1[0]
mov bl, operand_sign2[0]
cmp cl, bl
je outputSign_is_pos2
mov outputSign[0],"-"
jmp endsigndiv
outputSign_is_pos2:
mov outputSign[0]," "
endsigndiv:
mov outputSign[1],"$"
ret
div_nos endp
;prints the error message indicating that an operand's value is greater that 65536
input_out_of_range proc
call print_new_line
mov dx, offset error_range
mov ah, 09h
int 21h
call print_new_line
mov dx, offset redo
mov ah, 09h
int 21h
call print_new_line
jmp start_program
input_out_of_range endp
;prints the error message indicating that an overflow has occured
overflow proc
call print_new_line
mov dx, offset error_overflow
mov ah, 09h
int 21h
call print_new_line
mov dx, offset redo
mov ah, 09h
int 21h
call print_new_line
jmp start_program
overflow endp
;checks if there is a letter in the input
isalpha proc
mov cx , inputlen
mov si, 0
start:
mov ax, 0
mov al, inputData[si]
call check_if_alpha
jz input_is_alpha
inc si
loop start
ret
isalpha endp
;prints the error message indicating that there are letter/s or symbol/s in the input
input_is_alpha proc
call print_new_line
mov dx, offset error_alpha
mov ah, 09h
int 21h
call print_new_line
mov dx, offset redo
mov ah, 09h
int 21h
call print_new_line
call start_program
input_is_alpha endp
;prints the error message indicating that the syntax of the input is wrong
invalid_input proc
call print_new_line
mov dx, offset error_invalidinput
mov ah, 09h
int 21h
call print_new_line
mov dx, offset redo
mov ah, 09h
int 21h
call print_new_line
call start_program
;ret
invalid_input endp
;prints the decimal equivalent of the answer
print_decimal_output proc
mov dx, offset outputDecimal
mov ah, 9h
int 21h

mov ax, finalAnswer
mov bl, 0ah
div bl
mov ah, 0
mov rev_ascii_decimal,al

mov bx, 0
mov cx, dec_len
convertdecloop:
mov al, rev_ascii_decimal[bx]
add al, 30h
mov rev_ascii_decimal, al
inc bx
loop convertdecloop

mov ax, 0
mov bx, 0
mov cx, dec_len
;mov bx, dec_len
decimalloop:
mov ah, 02h
mov dl, rev_ascii_decimal[bx]
int 21h
inc bx
loop decimalloop

call print_new_line
call print_binary_output
print_decimal_output endp

;prints the binary equivalent of the number
print_binary_output proc
mov dx, offset outputBinary
mov ah, 9h
int 21h

mov dx, offset outputSign
mov ah, 02h
int 21h

mov bx, 0
mov ax, finalAnswer
convertbinloop:
mov cl, 2
div cl
cmp ah, 0
je zerobin
mov rev_ascii_binary[bx],"1"
zerobin:
mov rev_ascii_binary[bx],"0"
nextbin:
inc bx
cmp bx, bin_len
je endbinary
jmp convertbinloop

endbinary:
;mov bx, bin_len
;mov bx, 4
mov rev_ascii_binary[bx],"$"
;mov ax, 0
mov bx, 4
;mov cx, bin_len
mov cx, 4
;mov bx, bin_len

binaryloop:
mov ah, 02h
mov dl, rev_ascii_binary[bx]
int 21h
dec bx
loop binaryloop

call print_new_line
ret
;call print_octal_output
print_binary_output endp
;prints the octal equivalent of the answer
print_octal_output proc
mov dx, offset outputOctal
mov ah, 9h
int 21h

mov ax, finalAnswer
convertoctloop:
mov cl, 16
div cl
cmp ah, 0
je zerooct
mov rev_ascii_octal[bx], "1"
jmp nextoct
zerooct:
mov rev_ascii_octal[bx], "0"
nextoct:
inc bx
cmp bx, oct_len
je endoct
jmp convertoctloop
endoct:
mov rev_ascii_octal[bx],"$"

mov ax, 0
mov bx, 0
mov cx, oct_len
mov bx, oct_len
octalloop:
mov ah, 02h
mov dl, rev_ascii_octal[bx]
int 21h
inc bx
loop octalloop

call print_new_line
call print_hex_output
print_octal_output endp
;prints the hexidecimal equivalent of the number
print_hex_output proc
mov dx, offset outputHex
mov ah, 9h
int 21h
mov ax, finalAnswer
mov bx, 0
converthexloop:
mov cl, 16
div cl
cmp ah, 0
je zerohex
mov rev_ascii_hexadecimal[bx], "1"
jmp nexthex
zerohex:
mov rev_ascii_hexadecimal[bx], "0"
nexthex:
inc bx
cmp bx, hex_len
je endhex
jmp converthexloop
endhex:
mov rev_ascii_hexadecimal[bx],"$"

mov ax, 0
mov cx, hex_len
mov bx, hex_len
hexadecimalloop:
mov ah, 02h
mov dl, rev_ascii_hexadecimal[bx]
int 21h
inc bx
loop hexadecimalloop

call end_program
print_hex_output endp

;prints a new line
print_new_line proc
mov dx, offset newline
mov ah, 09h
int 21h
ret
print_new_line endp
check_if_alpha proc
push ax
and al, 11011111b
cmp al, 'A'
jb B1
cmp al, 'Z'
ja B1
test ax, 0
B1: pop ax
ret
check_if_alpha endp

check_if_digit proc
cmp al, '0'
jb A1
cmp al, '9'
ja A1
test ax, 0
A1: ret
check_if_digit endp
;terminates the program
end_program proc
mov ah, 4ch
int 21h
end_program endp
main proc
mov ax, @data
mov ds, ax

call setcolors

start_program:
call prompt_for_input
call print_new_line

mov dx, offset inputData
mov ah, 09h
int 21h

call isalpha

call print_new_line

call get_operators_and_operands

call convert_operands_to_decimal

call check_operator

call print_decimal_output
call end_program
main endp

end main

If you want help, maybe you should ask a question.

need to convert the input to decimal operands so that i can do arithmetic operations on them

If you want help, maybe you should ask a question.

Assuming some input character is in AL, then doing SUB AL,'0' is the traditional way to convert '0'-'9' to 0-9

This isn't your code is it?

actually it is. if the input is 123 and i subtract 30h from each character since the hex value of ex. 3 is 33h then i would just be removing the ascii value of that single digit right? it still wouldn't be the decimal 123, it would just be the digits 1, 2 and 3 not in ascii. my problem is converting the whole input as a single decimal number.

Assuming some input character is in AL, then doing SUB AL,'0' is the traditional way to convert '0'-'9' to 0-9

This isn't your code is it?

Multiply by 10, add current and repeat until you're out of numbers

; Camille Bianca T. Alipio
; cs21 FTXY Machine Problem
; March 14, 2008


.model small
.stack 64
.data
msg1 db "Enter first operand: $"
msg2 db "Enter second operand: $"
msg3 db "Enter operation (+, -, *, /): $"
msg4 db "Result: "
negative db "- $"
greaterMsg db "Constant too large (operand must be <= 65535).$"
invalidFormatMsg db "Invalid number format.$"
invalidOperatorMsg db "Invalid operator.$"
divbyzeroMsg db "Error, division by zero.$"
newline db 13,10,'$'

number db "65535"

sum db '000000', '$'
diff db '00000', '$'
product db '0000000000', '$'
powers dw 1, 10, 100, 1000, 10000
temp dw ?
quotient db ?

num1 label byte
max1 db 20
len1 db ?
s1 db 20 dup(?)

num2 label byte
max2 db 20
len2 db ?
s2 db 20 dup(?)

.code
main proc

mov ax, @data
mov ds, ax
mov es, ax

mov ah, 09h
mov dx, offset msg1
int 21h

mov dx, offset max1
mov ah, 0ah
int 21h

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

mov cx, 32
lea si, num1
add si, 2
lea di, number

mov cl, len1
mov ch, 00h

cmp cl, 5
jb LESSTHAN
ja GREATERTHAN
je COMP

COMP:
mov al, [si]
mov ah, 00h
mov dl, [di]
mov dh, 00h
cmp ax, dx
ja GREATERTHAN
jb LESSTHAN
inc si
inc di
loop comp

je EQUAL

EQUAL:
cmp cl, 5
je GETNUM2

LESSTHAN:
cmp cl, 5
je GETNUM2
jb GETNUM2

GREATERTHAN:
mov ah, 09h
lea dx, greaterMsg
int 21h
mov ah, 4ch
int 21h

GETNUM2:
mov ah, 09h
mov dx, offset msg2
int 21h

mov dx, offset max2
mov ah, 0ah
int 21h

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

mov cx, 32
lea si, num2
add si, 2
lea di, number

mov cl, len2
mov ch, 00h

cmp cl, 5
jb LESSTHAN2
ja GREATERTHAN2
je COMP2

COMP2:
mov al, [si]
mov ah, 00h
mov dl, [di]
mov dh, 00h
cmp ax, dx
ja GREATERTHAN2
jb LESSTHAN2
inc si
inc di
loop comp2

je EQUAL2

EQUAL2:
cmp cl, 5
je GETOPERATOR

LESSTHAN2:
cmp cl, 5
je GETOPERATOR
jb GETOPERATOR

GREATERTHAN2:
mov ah, 09h
lea dx, greaterMsg
int 21h
mov ah, 4ch
int 21h

GETOPERATOR:
mov ah, 09h
mov dx, offset msg3
int 21h

mov ah, 1 ;gets 1 character from user
int 21h

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

cmp al, '+'
jz addhere
jnz comp3

comp3:
cmp al, '-'
jz subtracthere
jnz comp4

comp4:
cmp al, '*'
jz multiplyhere

addhere:
clc ;clear carry flag
lea si, num1+6
lea di, num2+6
lea bx, sum+7
mov cx, 7

ADDITION:
mov ah, 00
mov al, [si]
adc al, [di]
aaa
mov [bx], al
dec si
dec di
dec bx
loop ADDITION

mov [bx], ah ;store carry out'

lea bx, sum+7
mov cx, 8

DISP:
or byte ptr[bx], 30h
dec bx
loop DISP

mov ah, 09h
lea dx, sum
int 21h
mov ah, 4ch
int 21h

subtracthere:
mov bx, 0h
mov bl, len1
mov s1[bx], '$'

mov bx, 0h
mov bl, len2
mov s2[bx], '$'

mov cx, 32
lea si, num1
add si, 2
lea di, num2
add di, 2

mov cl, len1
mov ch, len2

cmp ch, cl
jb subhere
ja GREATERTHAN3

je COMP5

COMP5:
mov al, [si]
mov ah, 00h
mov dl, [di]
mov dh, 00h
cmp ax, dx
ja subhere
jb GREATERTHAN3
inc si
inc di
loop comp5

je subhere

GREATERTHAN3:
clc ;clear carry flag
lea si, num2+6
lea di, num1+6
lea bx, diff+6
mov cx, 6

SUBTRACTION1:
mov ah, 00
mov al, [si]
sbb al, [di]
aas
mov [bx], al
dec si
dec di
dec bx
loop SUBTRACTION1

mov [bx], ah ;store carry out'

lea bx, diff+6
mov cx, 7

DISP2:
or byte ptr[bx], 30h
dec bx
loop DISP2

mov ah, 09h
lea dx, negative
int 21h
mov ah, 09h
lea dx, diff
int 21h
mov ah, 4ch
int 21h

subhere:

clc ;clear carry flag
lea si, num1+6
lea di, num2+6
lea bx, diff+6
mov cx, 6

SUBTRACTION:
mov ah, 00
mov al, [si]
sbb al, [di]
aas
mov [bx], al
dec si
dec di
dec bx
loop SUBTRACTION

mov [bx], ah ;store carry out'

lea bx, diff+6
mov cx, 7

DISP1:
or byte ptr[bx], 30h
dec bx
loop DISP1

mov ah, 09h
lea dx, diff
int 21h
mov ah, 4ch
int 21h

multiplyhere:
clc
lea si, num2 + 4 ;make si point to the last digit
mov cx, 5 ;the string length
call conversion

call multiplication

lea dx, product
call printout

mov ah, 4ch
int 21h


main endp

conversion proc
lea di, powers

mov ax, 0000
mov dx, 0000
mov bx, 0000

START:
mov ax, [si]
mov ah, 00
mov dx, [di]
dec si
inc di
inc di
sub al, 30h ;to change from ascii format to BCD format
mul dx
add bx, ax
loop START

ret
conversion endp

multiplication proc
mov temp, bx ;bx holds result of conversion procedure
lea si, num1 + 4 ;this determines how many times we will add
lea di, num1 + 4 ;the multiplicand to itself
lea bx, product + 9
call addition1

mov cx, temp ;load to cx for looping purposes
multiply:
lea si, product + 9
lea di, num1 + 4
lea bx, product + 9
mov temp, cx ;since procedure addition uses cx also, store current value
call addition1 ;to temp
mov cx, temp ;and reload it after addition is terminated
loop multiply
multiplication endp

addition1 proc
mov cx, 5

ADDLOOP:
mov ah, 00
mov al, [si]
adc al, [di]
aaa
mov [bx], al
dec si
dec di
dec bx
loop ADDLOOP
mov cx, 10


CARRYOUT:
mov ax, 0
adc al,[bx]
aaa
mov [bx],al
dec bx
loop CARRYOUT


lea bx, product + 9
mov cx, 10
DISP3:
or byte ptr[bx], 30h
dec bx
loop DISP3
ret
addition1 endp

printout proc
mov ah, 09h
int 21h
ret
printout endp

end main


here's my code.i'm still working on multiplication and division

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.