Program Example 1



Program Example 1

This example need 3 files, tryitoa.asm, atoi.asm, and itoa.asm.

File : tryitoa.asm

title TRYITOA -- Demonstration Program for ITOA and ATOI

page 55,132

; TRYITOA.ASM --- Simple Interactive Demo of ATOI and ITOA routines

;

; Copyright (C) 1989 Ray Duncan

;

_TEXT segment word public 'CODE'

_TEXT ends

_DATA segment word public 'DATA'

_DATA ends

STACK segment para stack 'STACK'

STACK ends

DGROUP group _DATA,STACK

assume cs:_TEXT,ds:DGROUP,ss:DGROUP,es:DGROUP

cr equ 0dh ; ASCII carriage return

lf equ 0ah ; ASCII line feed

extrn itoa:near

extrn atoi:near

_TEXT segment

main proc near

mov ax,_DATA ; or mov ax,DGROUP , adjust DS

mov ds,ax ; addressable...

mov es,ax

main1: mov dx,offset prompt ; display a prompt

mov cx,p_len ; to the user...

mov bx,1 ; "Enter value: "

mov ah,40h

int 21h

jc main2 ; if error, just exit

mov dx,offset inbuff ; read keyboard entry

mov cx,80 ; from the user...

mov bx,0

mov ah,3fh

int 21h

jc main2 ; if error, just exit

cmp ax,2 ; did he enter anything?

je main2 ; empty line, exit

mov dx,offset display ; display "You entered: "

mov cx,d_len ; to the user...

mov bx,1

mov ah,40h

int 21h

jc main2 ; if error, just exit

mov si,offset inbuff ; convert convert user's

call atoi ; input to binary in AX

mov cx,10 ; convert value in AX back

mov si,offset outbuff ; to ASCII string for output

call itoa

mov cx,ax ; CX = string length

mov dx,si ; DS:DX = string address

mov bx,1 ; now display converted

mov ah,40h ; ASCII string...

int 21h ;

jc main2 ; if error, just exit

jmp main1 ; do it again...

main2: mov ax,4c00h ; final exit to MS-DOS

int 21h

main endp

_TEXT ends

_DATA segment

prompt db cr,lf,lf,'Enter a number: '

p_len equ $-prompt

display db cr,lf,lf,'You entered: '

d_len equ $-display

inbuff db 80 dup (?)

outbuff db 80 dup (?)

_DATA ends

STACK segment para stack 'STACK'

db 128 dup (?)

STACK ends

end main

File : atoi.asm

title ATOI - ASCII to integer

page 55,132

; ATOI.ASM --- Convert ASCII string to 16-bit decimal integer.

;

; Copyright (C) 1989 Ray Duncan

;

; Call with: DS:SI = address of string,

; where 'string' is in the form

; [whitespace][sign][digits]

;

; Returns: AX = result

; DS:SI = address+1 of terminator

;

; Destroys: Nothing

;

; Like the C runtime library function 'atoi', this routine gives no

; warning of overflow, and terminates on the first invalid character.

_TEXT segment word public 'CODE'

_TEXT ends

_DATA segment word public 'DATA'

_DATA ends

STACK segment para stack 'STACK'

STACK ends

DGROUP group _DATA,STACK

assume cs:_TEXT,ds:DGROUP,ss:DGROUP,es:DGROUP

blank equ 20h ; ASCII blank character

tab equ 09h ; ASCII tab character

_TEXT segment

public atoi

atoi proc near

push bx ; save registers

push cx

push dx

xor bx,bx ; initialize forming answer

xor cx,cx ; initialize sign flag

atoi1: lodsb ; scan off whitespace

cmp al,blank ; ignore leading blanks

je atoi1

cmp al,tab ; ignore leading tabs

je atoi1

cmp al,'+' ; if + sign, proceed

je atoi2

cmp al,'-' ; is it - sign?

jne atoi3 ; no, test if numeric

dec cx ; was - sign, set flag

; for negative result

atoi2: lodsb ; get next character

atoi3: cmp al,'0' ; is character valid?

jb atoi4 ; jump if not '0' to '9'

cmp al,'9'

ja atoi4 ; jump if not '0' to '9'

and ax,0fh ; isolate lower four bits

xchg bx,ax ; previous answer x 10

mov dx,10

mul dx

add bx,ax ; add this digit

jmp atoi2 ; convert next digit

atoi4: mov ax,bx ; put result into AX

jcxz atoi5 ; jump if sign flag clear

neg ax ; make result negative

atoi5: pop dx ; restore registers

pop cx

pop bx

ret ; back to caller

atoi endp

_TEXT ends

end

File : itoa.asm

title ITOA --- Convert 16-bit integer to ASCII

page 55,132

; ITOA.ASM --- Convert 16-bit integer to ASCII string

;

; Copyright (C) 1989 Ray Duncan

;

; Call with: AX = 16-bit integer

; DS:SI = buffer to receive string,

; must be at least 6 bytes long

; CX = radix

;

; Returns: DS:SI = address of converted string

; AX = length of string

;

; Destroy: Nothing

;

; Since test for value = zero is made after a digit

; has been stored, the resulting string will always

; contain at least one significant digit.

_TEXT segment word public 'CODE'

_TEXT ends

_DATA segment word public 'DATA'

_DATA ends

STACK segment para stack 'STACK'

STACK ends

DGROUP group _DATA,STACK

assume cs:_TEXT,ds:DGROUP,ss:DGROUP,es:DGROUP

_TEXT segment

public itoa

itoa proc near

push dx

add si,6 ; advance to end of buffer

push si ; and save that address.

or ax,ax ; test sign of 16-bit value,

pushf ; and save sign on stack.

jns itoa1 ; jump if value was positive.

neg ax ; find absolute value.

itoa1: mov dx,0 ; divide value by radix to extract

div cx ; next digit for forming string

add dl,'0' ; convert remainder to ASCII digit

cmp dl,'9' ; in case converting to hex ASCII,

jle itoa2 ; jump if in range 0-9,

add dl,'A'-'9'-1 ; correct digit if in range A-F

itoa2: dec si ; back up through buffer

mov [si],dl ; store this character into string

or ax,ax

jnz itoa1 ; no, convert another digit

popf ; was original value negative?

jns itoa3 ; no, jump

dec si ; yes, store sign into output

mov byte ptr [si],'-'

itoa3: pop ax ; calculate length of string

sub ax,si

pop dx

ret ; back to caller

itoa endp

_TEXT ends

end

How to Compile and link

masm tryitoa,tryitoa;

masm atoi,atoi;

masm itoa,itoa;

link tryitoa+atoi+itoa,tryitoa;

Test

tryitoa

Enter a number : 12

You entered : 12

Enter a number : -56

You entered : -56

Note! 12 and –56 are what you enter, to stop just press enter

Make Library

ในกรณีที่เราต้องการสร้าง library ไว้เก็บ proc atoi และ itoa ไว้ใช้ในภายหลัง โดยไม่ต้อง แปลใหม่ทุกครั้ง ทำได้ ดังนี้

masm atoi,atoi;

masm itoa,itoa;

lib

Library name:mylib

Library does not exist, Create?y

operations:atoi+itoa

List file:

ท่านจะได้ library ชื่อ mylib.lib เก็บ proc atoi และ itoa ตามต้องการ

เมื่อต้องการใช้ สามารถ link ได้ ดังนี้

masm tryitoa,tryitoa;

link tryitoa,tryitoa,,mylib;

ถ้าท่านมี assembler version ที่สูงขึ้น และรู้จัก directive dosseg, .model, .code, .data, .stack สามารถเขียนโปรแกรมข้างต้นใหม่ ได้ดังนี้

File : tryitoa.asm

title TRYITOA -- Demonstration Program for ITOA and ATOI

page 55,132

; TRYITOA.ASM --- Simple Interactive Demo of ATOI and ITOA routines

;

; Copyright (C) 1989 Ray Duncan

;

dosseg

.model small

cr equ 0dh ; ASCII carriage return

lf equ 0ah ; ASCII line feed

extrn itoa:near

extrn atoi:near

.code

main proc near

mov ax,@data ; adjust DS

mov ds,ax ; addressable...

mov es,ax

main1: mov dx,offset prompt ; display a prompt

mov cx,p_len ; to the user...

mov bx,1 ; "Enter value: "

mov ah,40h

int 21h

jc main2 ; if error, just exit

mov dx,offset inbuff ; read keyboard entry

mov cx,80 ; from the user...

mov bx,0

mov ah,3fh

int 21h

jc main2 ; if error, just exit

cmp ax,2 ; did he enter anything?

je main2 ; empty line, exit

mov dx,offset display ; display "You entered: "

mov cx,d_len ; to the user...

mov bx,1

mov ah,40h

int 21h

jc main2 ; if error, just exit

mov si,offset inbuff ; convert convert user's

call atoi ; input to binary in AX

mov cx,10 ; convert value in AX back

mov si,offset outbuff ; to ASCII string for output

call itoa

mov cx,ax ; CX = string length

mov dx,si ; DS:DX = string address

mov bx,1 ; now display converted

mov ah,40h ; ASCII string...

int 21h ;

jc main2 ; if error, just exit

jmp main1 ; do it again...

main2: mov ax,4c00h ; final exit to MS-DOS

int 21h

main endp

.data

prompt db cr,lf,lf,'Enter a number: '

p_len equ $-prompt

display db cr,lf,lf,'You entered: '

d_len equ $-display

inbuff db 80 dup (?)

outbuff db 80 dup (?)

.stack 128

end main

File : atoi.asm

title ATOI - ASCII to integer

page 55,132

; ATOI.ASM --- Convert ASCII string to 16-bit decimal integer.

;

; Copyright (C) 1989 Ray Duncan

;

; Call with: DS:SI = address of string,

; where 'string' is in the form

; [whitespace][sign][digits]

;

; Returns: AX = result

; DS:SI = address+1 of terminator

;

; Destroys: Nothing

;

; Like the C runtime library function 'atoi', this routine gives no

; warning of overflow, and terminates on the first invalid character.

dosseg

.model small

blank equ 20h ; ASCII blank character

tab equ 09h ; ASCII tab character

.code

public atoi

atoi proc near

push bx ; save registers

push cx

push dx

xor bx,bx ; initialize forming answer

xor cx,cx ; initialize sign flag

atoi1: lodsb ; scan off whitespace

cmp al,blank ; ignore leading blanks

je atoi1

cmp al,tab ; ignore leading tabs

je atoi1

cmp al,'+' ; if + sign, proceed

je atoi2

cmp al,'-' ; is it - sign?

jne atoi3 ; no, test if numeric

dec cx ; was - sign, set flag

; for negative result

atoi2: lodsb ; get next character

atoi3: cmp al,'0' ; is character valid?

jb atoi4 ; jump if not '0' to '9'

cmp al,'9'

ja atoi4 ; jump if not '0' to '9'

and ax,0fh ; isolate lower four bits

xchg bx,ax ; previous answer x 10

mov dx,10

mul dx

add bx,ax ; add this digit

jmp atoi2 ; convert next digit

atoi4: mov ax,bx ; put result into AX

jcxz atoi5 ; jump if sign flag clear

neg ax ; make result negative

atoi5: pop dx ; restore registers

pop cx

pop bx

ret ; back to caller

atoi endp

end

File : itoa.asm

title ITOA --- Convert 16-bit integer to ASCII

page 55,132

; ITOA.ASM --- Convert 16-bit integer to ASCII string

;

; Copyright (C) 1989 Ray Duncan

;

; Call with: AX = 16-bit integer

; DS:SI = buffer to receive string,

; must be at least 6 bytes long

; CX = radix

;

; Returns: DS:SI = address of converted string

; AX = length of string

;

; Destroy: Nothing

;

; Since test for value = zero is made after a digit

; has been stored, the resulting string will always

; contain at least one significant digit.

dosseg

.model small

.code

public itoa

itoa proc near

push dx

add si,6 ; advance to end of buffer

push si ; and save that address.

or ax,ax ; test sign of 16-bit value,

pushf ; and save sign on stack.

jns itoa1 ; jump if value was positive.

neg ax ; find absolute value.

itoa1: mov dx,0 ; divide value by radix to extract

div cx ; next digit for forming string

add dl,'0' ; convert remainder to ASCII digit

cmp dl,'9' ; in case converting to hex ASCII,

jle itoa2 ; jump if in range 0-9,

add dl,'A'-'9'-1 ; correct digit if in range A-F

itoa2: dec si ; back up through buffer

mov [si],dl ; store this character into string

or ax,ax

jnz itoa1 ; no, convert another digit

popf ; was original value negative?

jns itoa3 ; no, jump

dec si ; yes, store sign into output

mov byte ptr [si],'-'

itoa3: pop ax ; calculate length of string

sub ax,si

pop dx

ret ; back to caller

itoa endp

end

How to Compile and link

tasm tryitoa,tryitoa;

tasm atoi,atoi;

tasm itoa,itoa;

tlink tryitoa+atoi+itoa,tryitoa;

กรณี ATOI.ASM ถ้าต้องการตรวจสอบว่าค่าอยู่นอกช่วงหรือไม่ (-32768 – 32767) เขียนใหม่ได้ดังนี้

title ATOI - ASCII to integer

page 55,132

; ATOI.ASM --- Convert ASCII string to 16-bit decimal integer and check range.

;

; Call with: DS:SI = address of string,

; where 'string' is in the form

; [whitespace][sign][digits]

;

; Returns: AX = result

; DS:SI = address+1 of terminator

; OF = 1 out of range (invalid) OF = 0 valid

;

; Destroys: Nothing

;

; Like the C runtime library function 'atoi', this routine gives no

; warning of overflow, and terminates on the first invalid character.

blank equ 20h ; ASCII blank character

tab equ 09h ; ASCII tab character

_TEXT segment word public 'CODE'

assume cs:_TEXT

public atoi

atoi proc near

push bx ; save registers

push cx

push dx

xor bx,bx ; initialize forming answer

xor cx,cx ; initialize sign flag

atoi1: lodsb ; scan off whitespace

cmp al,blank ; ignore leading blanks

je atoi1

cmp al,tab ; ignore leading tabs

je atoi1

cmp al,'+' ; if + sign, proceed

je atoi2

cmp al,'-' ; is it - sign?

jne atoi3 ; no, test if numeric

dec cx ; was - sign, set flag

; for negative result

atoi2: lodsb ; get next character

atoi3: cmp al,'0' ; is character valid?

jb atoi4 ; jump if not '0' to '9'

cmp al,'9'

ja atoi4 ; jump if not '0' to '9'

and ax,0fh ; isolate lower four bits

xchg bx,ax ; previous answer x 10

mov dx,10

mul dx

or dx,dx

jnz atoie

add bx,ax ; add this digit

jc atoie

; test 32768

cmp bx,8000h

ja atoie

jb atoi2

jcxz atoie

jmp atoi2

atoie: ; set OF bit 11 = 1 out of range

pushf

pop bx

or bx,0800h ; OF = 1

push bx

popf

jmp atoi5

atoi4: mov ax,bx ; put result into AX

or ax,ax ; OF = 0 valid

jcxz atoi5 ; jump if sign flag clear

neg ax ; make result negative

or ax,ax ; OF = 0 valid

atoi5: pop dx ; restore registers

pop cx

pop bx

ret ; back to caller

atoi endp

_TEXT ends

end

ตัวอย่างการเรียกใช้

title TRYITOA -- Demonstration Program for ITOA and ATOI

page 55,132

; TRYITOA.ASM --- Simple Interactive Demo of ATOI and ITOA routines

;

; Copyright (C) 1989 Ray Duncan

;

; To build: MAKE TRYITOA

cr equ 0dh ; ASCII carriage return

lf equ 0ah ; ASCII line feed

DGROUP group _DATA

_TEXT segment word public 'CODE'

assume cs:_TEXT,ds:_DATA

extrn itoa:near

extrn atoi:near

main proc near

mov ax,_DATA ; make our data segment

mov ds,ax ; addressable...

mov es,ax

main1: mov dx,offset prompt ; display a prompt

mov cx,p_len ; to the user...

mov bx,1 ; "Enter value: "

mov ah,40h

int 21h

jc main2 ; if error, just exit

mov dx,offset inbuff ; read keyboard entry

mov cx,80 ; from the user...

mov bx,0

mov ah,3fh

int 21h

jc main2 ; if error, just exit

cmp ax,2 ; did he enter anything?

je main2 ; empty line, exit

mov dx,offset display ; display "You entered: "

mov cx,d_len ; to the user...

mov bx,1

mov ah,40h

int 21h

jc main2 ; if error, just exit

mov si,offset inbuff ; convert convert user's

call atoi ; input to binary in AX

jo error

mov cx,10 ; convert value in AX back

mov si,offset outbuff ; to ASCII string for output

call itoa

mov cx,ax ; CX = string length

mov dx,si ; DS:DX = string address

mov bx,1 ; now display converted

mov ah,40h ; ASCII string...

int 21h ;

jc main2 ; if error, just exit

jmp main1 ; do it again...

error:

mov dx,offset err_msg ; display error message

mov cx,e_len ; to the user...

mov bx,1

mov ah,40h

int 21h

jmp main1

main2: mov ax,4c00h ; final exit to MS-DOS

int 21h

main endp

_TEXT ends

_DATA segment word public 'DATA'

prompt db cr,lf,lf,'Enter a number: '

p_len equ $-prompt

display db cr,lf,lf,'You entered: '

d_len equ $-display

err_msg db cr,lf,lf,'Error out of range'

e_len equ $-err_msg

inbuff db 80 dup (?)

outbuff db 80 dup (?)

_DATA ends

STACK segment para stack 'STACK'

db 128 dup (?)

STACK ends

end main

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download