How to recieve integer input from user

Started by xwiz, March 21, 2012, 05:29:39 PM

Previous topic - Next topic

dedndave

PrInp1: lodsb             ;same as MOV AL,[SI]/INC SI
        xor     al,30h    ;convert ASCII numeric character to binary
        cmp     al,9      ;if the result is greater than 9,
        ja      PrInp2    ;then it wasn't an ASCII numeric character

        cmp     dx,6553   ;16-bit registers hold 65535 max
        ja      PrInp4    ;if above 6553, multiplying by 10 would be overflow

;here, we multiply the saved (accumulated) value by 10, then add the new binary digit

        shl     dx,1      ;DX = 2 X val
        mov     bx,dx     ;BX = DX = 2 X val
        shl     dx,1      ;DX = 4 X val
        shl     dx,1      ;DX = 8 X val
        add     dx,bx     ;(8 X val) + (2 X val) = 10 X val
        add     dx,ax     ;add the new binary digit
        jc      PrInp4    ;if the result is greater than 65535, overflow

        loop    PrInp1    ;loop until done (same as DEC CX/JNZ PrInp1)

Force

I think You tried to  input just even numbers from keyboard in your code

using "DIV" is better way for it

.model small
.stack
.data


.code
start:
mov ax,@data
mov ds,ax

lp1:
mov ah, 1h
int 21h
cmp al,0Dh
je exit
cmp al,30h
jb go

cmp al,39h
ja go

mov bl,al
xor ah,ah
mov dl,2
div dl
cmp ah,0
jne go

mov dl,bl
mov ah,02h
int 21h

go:


mov dl,08h
mov ah,02h
int 21h

mov dl,00h
mov ah,02h
int 21h

mov dl,08h
mov ah,02h
int 21h


jmp lp1

exit:
mov ah, 4ch
int 21h


end start 
Never Stop Until You Are Better Than The Best

xwiz

Quote from: dedndave on March 21, 2012, 11:04:04 PM
PrInp1: lodsb             ;same as MOV AL,[SI]/INC SI
        xor     al,30h    ;convert ASCII numeric character to binary
        cmp     al,9      ;if the result is greater than 9,
        ja      PrInp2    ;then it wasn't an ASCII numeric character

        cmp     dx,6553   ;16-bit registers hold 65535 max
        ja      PrInp4    ;if above 6553, multiplying by 10 would be overflow

;here, we multiply the saved (accumulated) value by 10, then add the new binary digit

        shl     dx,1      ;DX = 2 X val
        mov     bx,dx     ;BX = DX = 2 X val
        shl     dx,1      ;DX = 4 X val
        shl     dx,1      ;DX = 8 X val
        add     dx,bx     ;(8 X val) + (2 X val) = 10 X val
        add     dx,ax     ;add the new binary digit
        jc      PrInp4    ;if the result is greater than 65535, overflow

        loop    PrInp1    ;loop until done (same as DEC CX/JNZ PrInp1)

:eek Thanks, that was really very helpful. :bg I wonder how long it will really take me to understand asm language.

xwiz

Quote from: Force on March 21, 2012, 11:10:09 PM
I think You tried to  input just even numbers from keyboard in your code

using "DIV" is better way for it

.model small
.stack
.data


.code
start:
mov ax,@data
mov ds,ax

lp1:
mov ah, 1h
int 21h
cmp al,0Dh
je exit
cmp al,30h
jb go

cmp al,39h
ja go

mov bl,al
xor ah,ah
mov dl,2
div dl
cmp ah,0
jne go

mov dl,bl
mov ah,02h
int 21h

go:


mov dl,08h
mov ah,02h
int 21h

mov dl,00h
mov ah,02h
int 21h

mov dl,08h
mov ah,02h
int 21h


jmp lp1

exit:
mov ah, 4ch
int 21h


end start 

@Force, thanks, the problem is not just receiving the inputs, after receiving the input I want to list even numbers from the supplied input to 0 or vice versa. If you could comment your code also, it will be very helpful. Thanks