Thursday, December 11, 2008

Compute character you input in Assembly

.model small
.stack 100h
cr equ 13d
lf equ 10d

.data
msg db cr,lf,'type anything:','$'
msg1 db cr,lf,'total characters entered:','$'

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

mov ax,offset msg
call puts

mov cx,0

write:
call getc
cmp al,13

je disp
inc cx

jmp write

disp:
mov ax,offset msg1
call puts
mov ax,cx
call putn

mov ax,4c00h
int 21h

putn:
push bx
push cx
push dx

mov dx,0
push dx
mov cx,10

cmp ax,0
jge calc_digits
neg ax
push ax

mov al,'-'
call putc
pop ax

calc_digits:
div cx
add dx,'0'
push dx
mov dx,0

cmp ax,0
jne calc_digits

disp_loop:
pop ax
cmp ax,0
je end_disp_loop

call putc
jmp disp_loop

end_disp_loop:
pop dx
pop cx
pop bx
ret


puts:
mov dx, ax
mov ah,9h
int 21h
ret

putc:
mov dl,al
mov ah,2h
int 21h
ret

getc:
mov ah,1h
int 21h
ret

end start

No comments: