Re: My final hope!! you lovely people.
- From: luke77561@xxxxxxxxx
- Date: 18 Mar 2006 09:50:31 -0800
Quick hack. This isn't working as author intended.
For one, I think it's needs the enter key eliminated
After number is input, you have to hit enter.
Always gives -18 for a result.
Prompt needs more info.
Like Input degrees C or F
User isn't a mind reader.
Also like Frank said, you need error checking.
Hope for the best but prepare for the worst.
; celsius.asm Temp conversion program March 2006
; Tasm code Sell See Us
; "If it crashes, it ain't my fault. :-)"
;
..model small
..stack 200h
..386
..data
; data goes here
prompt1 db 'Enter number:','$'
error1 db "Overflow",'$'
error2 db 'Input OVERFLOW','$'
sign db 0 ;sign of number
Instring db 4 dup(?)
..code
main:
;mov si,prompt1
;call PrintString
mov ax,@data
mov ds,ax
lea dx,prompt1 ; Print using interupts,
; we don't need to re-invent the wheel
mov ah,9
int 21h
mov Instring,4 ; max characters in input
mov dx,offset Instring ; store input here
mov ah,0ch ; flush keyboard buffer
mov al,0ah ; buffered keyboard input
int 21h
mov di,offset Instring
call ReadString
call CRLF ;INPUT Instring$
mov si,offset Instring
call StrToNum ;ax = VAL(InString$)
; ------- ax = (int)((ax - 32) * 5 / 9) -----------
sub ax,32 ;ax = ax - 32
mov bx,5
imul bx ;ax = ax * 5
mov bx,9
idiv bx ;ax = ax / 9 (dx = remainder, ax =
quotient)
xchg ax,dx ;now ax = remainder and dx = quotient
; round up the value
mov bl,5
idiv bl ;al = al / bl
cbw ;convert byte to word
add ax,dx ;ax = ax + quotient
mov si,offset Instring
call NumToStr ;InString$ = STRING$(ax)
mov si,offset Instring
call PrintString
call CRLF ;PRINT InString$
mov ax,4C00h
int 21h
;** READ KEYBOARD TO STRING POINTED TO BY DI **
;** ReadString(SI) **
ReadString proc
push dx
push ax
push di
push bx
mov bx,di
NextChar:
sub ah,ah
int 16h ;wait for key function
cmp al,13 ;[ENTER] key?
jz Done
cmp al,8 ;[<-] key?
jz BackSpace
mov [di],al ;save character
inc di
mov dl,al ;
mov ah,2
int 21h ;echo character
jmp NextChar
BackSpace:
cmp di,bx ;at beginning?
jz NextChar ;yep no back space
dec di
mov ah,2
mov dl,8
int 21h ;backspace
mov ah,2
mov dl,' ';
int 21h ;PRINT " ";
mov ah,2
mov dl,8
int 21h ;backspace
jmp NextChar
Done:
sub al,al ;clear al
mov [di],al ;null string
pop bx
pop di
pop ax
pop dx
ret
endp
;** PRINT STRING POINTED TO BY SI **
PrintString proc
push dx
push ax
push si
PrintLoop:
mov al,[si]
inc si
or al,al ;test for al=0
jz Finish
mov dl,al
mov ah,2
int 21h ;print character
jmp PrintLoop
Finish:
pop si
pop ax
pop dx
ret
endp
CRLF proc
push ax
push dx
mov ah,2
mov dl,13
int 21h
mov dl,10 ;lf
int 21h
pop dx
pop ax
ret
endp
;** Convert string pointed to by SI to number in ax **
StrToNum proc
push bx
push cx
push dx
push si
mov BYTE [sign],0 ;assume positive number
sub cx,cx ;cx = 0
mov bx,10 ;base 10
mov al,[si]
cmp al,'-'
jne NextDigit ;positive number
inc si ;skip '-' character
mov BYTE [sign],1 ;negative number
NextDigit:
mov al,[si] ;get digit
inc si
sub ah,ah ;ah = 0
sub al,'0' ;convert to 0-9 value
jc NotDecDigit
cmp al,10 ;
ja NotDecDigit
xchg ax,cx ;ax = old value cx = new value
mul bx ;ax = ax * 10
add cx,ax ;add new result
jno NextDigit
call OverFlow
NotDecDigit:
mov ax,cx ;ax = result
cmp dl,BYTE [sign]
jz positive
neg ax ;was negative signed
positive:
pop si
pop dx
pop cx
pop bx
ret
endp
OverFlow proc
push ax
push bx
push cx
push dx
push si
call CRLF
mov si,offset error2
call PrintString
call CRLF
pop si
pop dx
pop cx
pop bx
pop ax
ret
endp
;** Convert number in ax to string pointed to by DI **
NumToStr proc
push ax
push dx
push cx
push bx
push di
mov bx,10
xor cx,cx
cmp ax,0
jge NotZero ;ax positive number
neg ax ;2's complement
mov BYTE ptr [di],'-'
inc di
NotZero:
xor dx,dx ;set upper word of N to 0
div bx ;N/10 and n mod 10
add dl,'0' ;remainder in dl convert to asc
push dx ;push one digit onto stack
inc cx ;count digits
or ax,ax ;n = 0?
jne NotZero
PopDigit:
pop ax ;last digit first
mov [di],al ;
inc di
loop PopDigit
sub al,al ;al = 0
mov [di],al ;null string
pop di
pop bx
pop cx
pop dx
pop ax
ret
endp
WaitKey proc
mov ah,7
int 21h
ret
endp
end main
.
- Follow-Ups:
- Re: My final hope!! you lovely people.
- From: JGCASEY
- Re: My final hope!! you lovely people.
- References:
- My final hope!! you lovely people.
- From: patrick_woflian
- My final hope!! you lovely people.
- Prev by Date: Re: Slow ass registry editor
- Next by Date: Re: Guillermito's Particles
- Previous by thread: Re: My final hope!! you lovely people.
- Next by thread: Re: My final hope!! you lovely people.
- Index(es):
Relevant Pages
|