Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

messy calculator code.. please try this out

49 views
Skip to first unread message

Tom Harbert

unread,
May 14, 2003, 9:30:51 PM5/14/03
to
hi i am coding a calculator in x86 assembly for an assignment, i am pretty
stuck i think i have gone into too much detail and basically im lost.. could
anyone possibly give me some tips on how to go about this or change the code
i have.. thanks here it is..

thankyou

..model small
..stack 100h
..data

input db 0,0,0,0,0,0,0,0

realNumber1 db ?
realOperator1 db ?
realNumber2 db ?
realOperator2 db ?

numberOfInputs db ?
lastInput db ?
answer db ?
valid db ?
operatorKeyPressed db 01d
equalsKeyPressed db ?


calc db "********************"
db "* *"
db "********************"
db "* 7 8 9 *"
db "* 4 5 6 *"
db "* 1 2 3 *"
db "* + - 0 *"
db "* * / = *"
db "********************"

..code


main proc
mov ax, @data ; move address of data segment to the ax
register
mov ds, ax ; move ax into the data segment register

mov ax, 03h
int 10h

mov ax, 0B800h
mov es, ax
xor di, di


call print_calc
mov cx, 10000d
general:
call get_char


cmp lastInput, 00011011b ; was the escape key pressed
jz quit ; if so jump to quit

call check_valid
call print_char
loop general


quit:
mov ax, 4C00h
int 21h
main endp


get_char proc
mov ah, 00h
int 16h
mov lastInput, al
mov si, 0
mov input[si], al
add si, 1

ret
get_char endp


beep proc
; control byte
mov al, 0B6h ; control byte
out 43h, al ; send control byte to control register
; load note
mov ax, 3043d ; mov appropriate array element into ax
register
out 42h, al ; send the low byte
mov al, ah ; move the high byte into the al
register
out 42h, al ; send the high byte
; turn speaker on
in al, 61h ; get current setting
or al, 00000011b ; set bits 1 and 0
out 61h, al ; send new value
; delay
push cx ; push original cx value onto stack
mov cx, 0fffh ; initialise cx register
ac:
push cx ; push original cx value onto stack
mov cx, 0fffh ; initialise cx register
ab:
nop ; no operation
loop ab ; decrement cx and continue until cx = 0
pop cx ; pop original cx value off stack
loop ac ; decrement cx and continue until cx = 0
pop cx ; pop original cx value off stack
; turn speaker off
in al, 61h ; get value from port 61h
and al, 11111100b ; reset bits 1 and 0
out 61h, al ; send new value
ret ; return
beep endp


print_calc proc
mov ah, 00001111b
mov si, 0
mov cx, 9
mov di, 0

zz:
push cx
mov cx, 20

go:
mov al, calc[si]
mov es:[di], al
add di, 1
mov es:[di], ah
add di, 1
add si, 1
loop go

pop cx
add di, 280

loop zz
ret
print_calc endp


check_valid proc
cmp lastInput, "1"
jz op2
cmp lastInput, "2"
jz op2
cmp lastInput, "3"
jz op2
cmp lastInput, "4"
jz op2
cmp lastInput, "5"
jz op2
cmp lastInput, "6"
jz op2
cmp lastInput, "7"
jz op2
cmp lastInput, "8"
jz op2
cmp lastInput, "9"
jz op2
cmp lastInput, "0"
jz op2
cmp lastInput, "+"
jz op1
cmp lastInput, "-"
jz op1
cmp lastInput, "*"
jz op1
cmp lastInput, "/"
jz op1
cmp lastInput, "="
jz op1

jmp bad1

op2:
mov operatorKeyPressed, 00d
jz ok1

op1:
cmp lastInput, "="
jz eq1
jmp eq2

eq1:
mov equalsKeyPressed, 01d

eq2:
cmp operatorKeyPressed, 01d
jz bad1

mov operatorKeyPressed, 01d

call make_number ; try to make number into one int

jmp ok1

ok1:
mov valid, 01h
add numberOfInputs, 01d
call beep
jmp done1

bad1:
mov valid, 00h

done1:
ret
check_valid endp


make_number proc
cmp numberOfInputs, 01d
jz oneNum
cmp numberOfInputs, 02d
jz twoNum
cmp numberOfInputs, 03d
jz threeNum
cmp numberOfInputs, 04d
jz fourNum
cmp numberOfInputs, 05d
jz fiveNum
cmp numberOfInputs, 06d
jz sixNum


oneNum:
push si
mov si, 1
mov al, input[si]
mov realNumber1, al
pop si

twoNum:
push si
mov si, 1
mov al, input[si]
mov ah, 10d
mul ah
mov realNumber1, al
pop si

mov di, 500
mov ah, 00001111b

mov al, realNumber1
mov es:[di], al
add di, 1
mov es:[di], ah
add di, 1


threeNum:

fourNum:

fiveNum:

sixNum:


ret
make_number endp


print_char proc

cmp valid, 01h
jz ok2

jmp bad2

ok2:
mov ah, 00001111b

cmp numberOfInputs, 01d
jz oneVal
cmp numberOfInputs, 02d
jz twoVal
cmp numberOfInputs, 03d
jz threeVal
cmp numberOfInputs, 04d
jz fourVal
cmp numberOfInputs, 05d
jz fiveVal
cmp numberOfInputs, 06d
jz sixVal
cmp numberOfInputs, 07d
jz sevenVal


jmp bad2

oneVal:
mov di, 332
jmp print1
twoVal:
mov di, 334
jmp print1
threeVal:
mov di, 336
jmp print1
fourVal:
mov di, 338
jmp print1
fiveVal:
mov di, 340
jmp print1
sixVal:
mov di, 342
jmp print1
sevenVal:
mov di, 344
jmp print1


print1:
mov al, lastInput
mov es:[di], al
add di, 1
mov es:[di], ah
add di, 1

bad2:
ret
print_char endp


end main

Al Borowski

unread,
May 17, 2003, 3:32:18 AM5/17/03
to
<snip>

aha - a fellow ITB420 student @ QUT. Here are my comments:


1)Don't try and do the whole thing at a time. Maybe get the display
going doing first (EG print calculator picture), then try to print a
number to the screen (Hint: google for "add 3" math bcd). Then try and
get number entry going. etc, etc.

2) Avoid 'magic number's - yes, we ment through that in the tutes, but
it makes sense. and why use binary when not necessary?

3) to get full marks, we need to use decmial places and negative
signs. It might be easier to use the FPU. Email me for more info.

3) RPN is easier to create then infix.

My effort can be found @
http://everest.fit.qut.edu.au/~n4403584/calc.htm

cheers,

Al

0 new messages