Number I/O

Printing numbers

;printing numbers from 0 to 9
; ascii 48 to 57 are 0 to 9
org 100h  

mov cx,47;starting from ascii 47

loop:       
    add cx,1
call print; calling sub routing print, to print value of cx
    cmp cx,57
jne loop

MOV AH,4CH ;terminating program
INT 21H

;printing characters
print:
mov dx,cx
mov ah,2h
int 21h
ret


Comparing two numbers

org 100h

jmp start ;jumping to start label

;declearing two variables
num1 dw 2
num2 dw 2


start:;program starts here

mov ax,[num1];moving num1 to ax register
mov bx,[num2];moving num2 to ax register
cmp ax,bx;comparing
jne different;if not equal jump to label named different

same:
mov dx , 'Y';print Y if key is found
jmp end

different:
mov dx , 'N';print N if key is not found

end:
; printing character Y or N
mov ah , 2h
int 21h

MOV AH,4CH ;terminating the program
INT 21H

If I assign num2 to be 3 than output will be as follows


Input and Output for Numbers and some commands

;This function shows how, to get number from keyboard
;It also shows how to print a number
;It also shows how to use mul and div instruction
;You can perform any computation with the input
;I am computing square of input here
data segment
    ten dw 10
    enter_number db 'Enter number =','$'
    new_line db 13,10,'$';it will help us in moving to next line,same as '\n'
ends

stack segment
    dw  32 dup(0)
ends

code segment

    start:
        mov ax,data
        mov ds,ax
        mov es,ax

        lea dx,enter_number;asking user to print number
        mov ah, 9
        int 21h
     
        call get_num ;getting number from user
     
        mov ax,bx
        mul bx
        push ax ;saving number on stack
     
        lea dx,new_line ;moving to next line
        mov ah, 9
        int 21h
           
        pop ax ;getting answer back from stack
        call print

    mov ax, 4ch ; terminating
    int 21h                                                    

;This function will print any number present in ax
    print:
        mov cx,0;cx keeps count of digits, it will be used while printing

        break_number:;break_number stores each digit seperately in stack
            add cx,1
         
            xor dx,dx;same as mov dx,0
;any thing present in front of div is divided with the value
;of ax register and the answer is stored in ax while
;reminder goes in dx register        
            div ten
         
            push dx;storing digits in stack seperately
         
            cmp ax,0;indicates no more digit left to store in stack
        jne break_number

        show_number:;show_number pops all digits and prints them
            pop ax
            add ax,48 ;converting to ascii code for displaying.
            mov ah, 0Eh
            int 10h
         
            sub cx,1
            jnz show_number
    ret
;getting number and stores it in bx register
   get_num:
        ;get input from keyboard
        MOV     AH, 1
        INT     21h

        cmp al,13       ;13 means enter
        je input_complete ;if enter is pressed than input is complete
            mov ah,0    ;clearing ah
            sub al,30h ;getting actual value
            push ax ;storing ax in stack
            mov ax,bx
;any thing present in front of mul is multiplied with the value
;of ax register and the answer is stored in ax                    
            mul ten
         
            mov bx,ax
            pop ax
;putting calculated value in bx, according to its position
; unit, decimal, hundred e.t.c        
            add bx,ax
        jmp get_num
        input_complete:
    ret      
ends
   

1 comment:

  1. If we want to print String (byte type) of characters, then how we will do?

    ReplyDelete