n00b2asm
asked on
convert numbers from ascii to int
hey there,
I would like to know how to read a large number such as "1024" etc from the keyboard into a buffer and then convert it to binary in ax.
I know what in c this would be very easy to do
i have thought of this code
mov ah,01h
int 21h
mov BUFF,al
but this reads in only 1 character . How would i go about reading in large numbers and storing them in either a variable or ax ??
I would like to know how to read a large number such as "1024" etc from the keyboard into a buffer and then convert it to binary in ax.
I know what in c this would be very easy to do
i have thought of this code
mov ah,01h
int 21h
mov BUFF,al
but this reads in only 1 character . How would i go about reading in large numbers and storing them in either a variable or ax ??
Sounds like homework to me. So just some thing to think about.
First you might want to check if it is a number digit.
And clean up the display when it isn't
When you look at the ascii codes for the numbers notice how they are structured.
consider the AND instruction.
consider that each additional digit requires that any previously collected number must be multiplied by 10.
You need three instructions for the actual conversion part.
Not counting any overflow (number too big) testing or data storage.
First you might want to check if it is a number digit.
And clean up the display when it isn't
When you look at the ascii codes for the numbers notice how they are structured.
consider the AND instruction.
consider that each additional digit requires that any previously collected number must be multiplied by 10.
You need three instructions for the actual conversion part.
Not counting any overflow (number too big) testing or data storage.
ASKER
this is the code that i have so far, the util.lib can be downloaded from here http://k4shif.netfirms.com/util.lib
for some reason the ascii to int conversion doesnt seem to be working.
;;program3.asm
title program3
.nolist
include bios.inc
include constants.inc
includelib util.lib
.list
.model small
.586
.stack 100h
.data
CR equ 0Ah
LF equ 0Dh
mess1 db CR,LF,"Please enter a number: "
messlen equ lengthof mess1
keybuff db 20,0,20 dup("*")
base dw 1
.code
extern PutDec:near
main proc
.startup
@Cls
@Setcsrpos 0,0
Outer:
mov ah,040h
mov bx,1
mov cx,messlen
lea dx,mess1
int 21h
call keyread
CheckSign:
cmp [keybuff+2],'-'
je Outer
mov al,[keybuff+1] ;keep the 8bit number in al, that is the strlen
sub ah,ah ;by doing this step, a 16 bit number is present in ax.
mov cx,ax ;and now we load the the strlen into the counter
mov di,ax ;
sub ax,ax
ascii2intLoop:
sub [keybuff+di],30h ;this now converts the ascii into an int.
add al,[keybuff+di]
sub ah,ah
mul base
imul bx,base,10
mov base,bx
dec di
dec cx
jnz ascii2intloop
call PutDec
.exit
main endp
keyread proc
lea dx,keybuff
mov ah,0ah
int 21h
ret
keyread endp
end
for some reason the ascii to int conversion doesnt seem to be working.
;;program3.asm
title program3
.nolist
include bios.inc
include constants.inc
includelib util.lib
.list
.model small
.586
.stack 100h
.data
CR equ 0Ah
LF equ 0Dh
mess1 db CR,LF,"Please enter a number: "
messlen equ lengthof mess1
keybuff db 20,0,20 dup("*")
base dw 1
.code
extern PutDec:near
main proc
.startup
@Cls
@Setcsrpos 0,0
Outer:
mov ah,040h
mov bx,1
mov cx,messlen
lea dx,mess1
int 21h
call keyread
CheckSign:
cmp [keybuff+2],'-'
je Outer
mov al,[keybuff+1] ;keep the 8bit number in al, that is the strlen
sub ah,ah ;by doing this step, a 16 bit number is present in ax.
mov cx,ax ;and now we load the the strlen into the counter
mov di,ax ;
sub ax,ax
ascii2intLoop:
sub [keybuff+di],30h ;this now converts the ascii into an int.
add al,[keybuff+di]
sub ah,ah
mul base
imul bx,base,10
mov base,bx
dec di
dec cx
jnz ascii2intloop
call PutDec
.exit
main endp
keyread proc
lea dx,keybuff
mov ah,0ah
int 21h
ret
keyread endp
end
maybe easier
byte buffer 8 Dup(0)
word result
.code
;assume ds is the data segment
mov byte ptr ds:[buffer],5 ; maxium number of bytes to accept in interrup
mov ah,10 ;max is 65535
mov dx,offset buffer
int 21h ;get buffered input until carrage return
xor bx,bx
mov bl,[buffer+1] ;number of bytes received
mov cx,1
mov si,offset buffer+2 ;point to input buffer
mov word ptr [result],0 ;zero out result
loop1:
mov al,[si+bx] ;get right most character - the lowest value
and ax,15 ;change ascii to hex for 0 - 9
mul cx ;multiply by the position factor
add [result],ax ;add to result
;you can also check here for an overflow
imul cx,10 ;multiply position factor by 10 each time
dec bx ;go back to the left most character
jne loop1
;finished with routine
byte buffer 8 Dup(0)
word result
.code
;assume ds is the data segment
mov byte ptr ds:[buffer],5 ; maxium number of bytes to accept in interrup
mov ah,10 ;max is 65535
mov dx,offset buffer
int 21h ;get buffered input until carrage return
xor bx,bx
mov bl,[buffer+1] ;number of bytes received
mov cx,1
mov si,offset buffer+2 ;point to input buffer
mov word ptr [result],0 ;zero out result
loop1:
mov al,[si+bx] ;get right most character - the lowest value
and ax,15 ;change ascii to hex for 0 - 9
mul cx ;multiply by the position factor
add [result],ax ;add to result
;you can also check here for an overflow
imul cx,10 ;multiply position factor by 10 each time
dec bx ;go back to the left most character
jne loop1
;finished with routine
Quite a bit different from your original code snipit.
Not too bad for a first try. You are somewhere in the ballpark.
Step through it with debug. See if what happens is what you expect to happen.
If I was your instructor, I would gripe about the use of the macro you didn't write, but thats just me.
Not too bad for a first try. You are somewhere in the ballpark.
Step through it with debug. See if what happens is what you expect to happen.
If I was your instructor, I would gripe about the use of the macro you didn't write, but thats just me.
ASKER
well after a little bit more thinking i came up with this code that does convert the ascii to decimal. Now i need to convert it to binary, store that result and then convert to string for output again.
I think i have the convert to binary bit, but dont know how to bring it back to a variable et al
;;program3.asm
title program3
.nolist
include bios.inc
include constants.inc
includelib util.lib
.list
.model small
.586
.stack 100h
.data
CR equ 0Ah
LF equ 0Dh
mess1 db CR,LF,"Please enter a number: "
messlen equ lengthof mess1
spacer db CR
spacerlen equ lengthof spacer
keybuff db 20,0,20 dup("*")
binbase dw 2d
.code
extern PutDec:near
main proc
.startup
@Cls
@Setcsrpos 0,0
Outer:
mov ah,040h
mov bx,1
mov cx,messlen
lea dx,mess1
int 21h
call keyread
CheckSign:
cmp [keybuff+2],'-'
je Outer
xor ax,ax
movzx cx,[keybuff+1]
mov di,2
Ascii2Loop:
xor [keybuff+di],30h
mov bl,[keybuff+di]
imul ax,10
add al,bl
inc di
dec cx
jnz Ascii2Loop
;at this point ax has the decimal value.
BinLoop:
idiv binbase
push dx
cmp ax,0
jnz BinLoop
.exit
main endp
keyread proc
lea dx,keybuff
mov ah,0ah
int 21h
ret
keyread endp
end
I think i have the convert to binary bit, but dont know how to bring it back to a variable et al
;;program3.asm
title program3
.nolist
include bios.inc
include constants.inc
includelib util.lib
.list
.model small
.586
.stack 100h
.data
CR equ 0Ah
LF equ 0Dh
mess1 db CR,LF,"Please enter a number: "
messlen equ lengthof mess1
spacer db CR
spacerlen equ lengthof spacer
keybuff db 20,0,20 dup("*")
binbase dw 2d
.code
extern PutDec:near
main proc
.startup
@Cls
@Setcsrpos 0,0
Outer:
mov ah,040h
mov bx,1
mov cx,messlen
lea dx,mess1
int 21h
call keyread
CheckSign:
cmp [keybuff+2],'-'
je Outer
xor ax,ax
movzx cx,[keybuff+1]
mov di,2
Ascii2Loop:
xor [keybuff+di],30h
mov bl,[keybuff+di]
imul ax,10
add al,bl
inc di
dec cx
jnz Ascii2Loop
;at this point ax has the decimal value.
BinLoop:
idiv binbase
push dx
cmp ax,0
jnz BinLoop
.exit
main endp
keyread proc
lea dx,keybuff
mov ah,0ah
int 21h
ret
keyread endp
end
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER