; ; CPASM02S.ASM ; written by Keith Fenske ; Saturday, 9 March 2002 ; Copyright (c) 2002 by Keith Fenske. All rights reserved. ; ; This is an Intel 80x86 assembly language program to convert an unsigned ; 16-bit binary number to printed decimal form. The number is declared as ; an arbitrary word in this program file; there is no input from the user. ; The converted decimal number will have leading zeros, so an unsigned ; number like 57 will print as "00057". This will be improved in the ; second version of this assignment. ; ; To test the program properly, the number should be changed several times ; and the program re-assembled and re-run. ; ; The required assembly concepts are: basic instructions, loops, and ; documentation for the DIV instruction. ; [BITS 16] ; set 16-bit code generation [ORG 0100H] ; set address of first instruction in COM file CR equ 0Dh ; carriage return character LF equ 0Ah ; line feed character [SECTION .text] ; start code (instruction) section start: ; ; Convert one decimal digit at a time to ASCII. We have to do this ; *backwards* by dividing by ten and using the remainder. ; mov ax,[number] ; put the unsigned number in the AX register mov bx,10 ; we will divide by ten mov cx,5 ; how many digits to do mov di,digits+4 ; address of last digit in output string convert: mov dx,0 ; clear DX before dividing DX:AX by BX div bx ; result: AX has quotient, DX has remainder add dl,'0' ; convert byte remainder to single-digit ASCII mov [di],dl ; save low-order byte as ASCII in output digits dec di ; address of previous digit in output string loop convert ; repeat until CX=0 (five times) ; ; Write the output string. ; mov bx,1 ; DOS file handle 1 is standard output mov cx,length ; length of string in bytes mov dx,string ; address of first byte in string mov ah,40h ; DOS function code for write string int 21h ; call DOS mov ah,4Ch ; DOS function code to exit program mov al,0 ; pass this value back as ERRORLEVEL int 21h ; call DOS (does not return) [SECTION .data] ; start data section ; ; The output string has several parts. We only change the part that has ; the digits. ; number dw 1342 ; 16-bit unsigned binary number string db "Unsigned decimal number is " digits db "99999" ; where decimal digits go db ".", CR, LF ; new line characters length equ $ - string ; ; End of CPASM02S.ASM ;