汇编程序设计:代码转换程序

程序说明
这是一个字母或数制之间的转化程序,5为主模块,调用模块1和模块7;模块1又调用模块2、3、4和6四个模块,其中:
  模块2实现小写字母向大写字母的转换
模块3实现大写字母向小写字母的转换
模块4实现二进制数向十六进制数的转换
模块6实现十六进制数向二进制数的转换
模块7实现十六进制数向十进制数的转换
按“q”键退出。
使用时,需将7个文件分别汇编,连接的方法为:5+1+2+3+4+6+7,生成可执行文件“5” 即可运行。

流程图:
汇编程序设计:代码转换程序_第1张图片
源代码:

main

public  info1,info2,bufa,bufb,buf,main,info0,info3
extrn  change1:far,change2:far,change3:far,change4:far
data segment
info0  db 0ah,0dh,'this is the change of letter to leter:$' 
info1  db 0ah,0dh,'please input string:$'
info2  db 0ah,0dh,'output string:$'
bufa   db  81
       db ?
       db 80 dup (?)
bufb  db 80 dup (?)
info3 db 0ah,0dh,'this is the change of digital to digital:',0ah,0dh,'$'
buf   db 0dh,0ah,'the number is:0100101010001111b',0ah,0dh,'the result is:$'
data ends
stack segment stack
     db 200 dup (?)
stack ends
code segment
main proc far
      assume ds:data,cs:code,ss:stack
      mov ax,data
      mov ds,ax
      
disp  macro m
      lea dx,m
      mov ah,9
      int 21h
      endm
      call change1
      call change2
      disp info3
      
      disp  buf
      call change3
      call change4
      ret
main endp
code ends
     end 

主程序:

extrn main:far,change5:far
stack segment para public 'stack'
      db 200 dup (0)
stack ends
data segment
;*****主程序中的提示信息*****
str1  DB 0AH,0DH, '********************************************$'
str2  DB 0AH,0DH, '**                                        **$'
str3  DB 0AH,0DH, '**            welcome     you !           **$'
str4  DB 0AH,0DH, '**                                        **$'
str5  DB 0AH,0DH, '** this is programme of shuzhizhuanghuan  **$'
str6  db 0AH,0DH, '**                                        **$'
str7  db 0AH,0DH, '**       press b to begin exchange!       **$'
str8  DB 0AH,0DH, '**           press q to exit !            **$'
str9  DB 0AH,0DH, '**                                        **$'
str10 DB 0AH,0DH, '********************************************$'
str11 db 0ah,0dh,'input error,please repess!','$'
data ends
code segment
     assume cs:code,ds:data,ss:stack
start: mov ax,data
      mov ds,ax
disp  macro m
      lea dx,m
      mov ah,9
      int 21h
      endm
      
      disp str1
      disp str3
      disp str4
      disp str5
      disp str6
      disp str7
      disp str8
      disp str9
      disp str10
lop:  mov ah,8
      int 21h
      cmp al,'b'
      jz  ltol
      cmp al,'q'
      jz exit
      disp str11
      jmp lop
ltol: call main
      call change5
      jmp lop
exit: mov ah,4ch
      int 21h
code  ends
      end start

change1

extrn info1:byte,info2:byte,bufa:byte,bufb:byte,info0:byte
public change1
code segment
  assume cs:code
lowtohigh proc near
   cmp al,'a'
   jb stop
   cmp al,'z'
   ja stop
   sub al,20h
stop :ret
lowtohigh endp
change1 proc far
   lea dx,info0
   mov ah,9
   int 21h

   lea dx,info1
   mov ah,9
   int 21h
   lea dx,bufa
   mov ah,10
   int 21h
   lea si,bufa+1
   lea di,bufb
   mov ch,0
   mov cl,[si]
   add si,1
next:mov al,[si]
     call lowtohigh
     mov [di],al
     inc si
     inc di
     loop next
     mov byte ptr [di],'$'
     lea dx,info2
     mov ah,9
     int 21h
     lea dx,bufb
     mov ah,9
     int 21h
     ret
change1 endp
  code ends
  end 

change2

extrn info1:byte,info2:byte,bufa:byte,bufb:byte
public change2
code segment
  assume cs:code
hightolow proc near
     cmp al,'A'
     jb  stop
     cmp al,'Z'
     ja stop
     add al,20h
stop:ret
hightolow endp
change2 proc far
     lea dx,info1
     mov ah,9
     int 21h
     lea dx,bufa
     mov ah,10
     int 21h
     lea si,bufa+1
     lea di,bufb
     mov ch,0
     mov cl,[si]
     inc si
next:mov al,[si]
     call hightolow
     mov [di],al
     inc si
     inc di
     loop next
     mov byte ptr [di] ,'$'
     lea dx,info2
     mov ah,9
     int 21h
     lea dx,bufb
     mov ah,9
     int 21h
     ret
change2 endp
  code ends
  end

change3

extrn buf:byte,info3:byte
public change3
code segment
change3 proc far
       assume cs:code
       sub ax,ax
       mov ch,4
       mov cl,4
       mov bx,4a8fh
rotate: rol bx,cl
        mov al,bl
        and al,0fh
        add al,30h
        cmp al,3ah
        jl printit
        add al,7h
printit: mov dl,al
         mov ah,2
         int 21h
         dec ch
         jnz rotate
         mov dl,'H'
         mov ah,2
         int 21h
         ret
change3  endp
code     ends
         end 

change4

public change4
data segment
buf db 0ah,0dh,'please input the four  hexnumber:$'
data ends
code segment
     assume cs:code,ds:data
change4 proc far
       mov ax,data
       mov ds,ax
       lea dx,buf
       mov ah,9
       int 21h
       mov bx,0
       mov ch,4
input: mov cl,4
       sal bx,cl
       mov ah,1
       int 21h
       cmp al,39h
       ja af
       and al,0fh
       jmp binary
af:    and al,0fh
       add al,9
binary: or bl,al
        dec ch
        cmp ch,0
        jne input
        mov dl,'H'
        mov ah,2
        int 21h
        mov dl,0ah
        mov ah,2
        int 21h
        mov dl,0dh
        mov ah,02
        int 21h
       mov cx,16
disp:  mov dl,0
       rol bx,1
       rcl dl,1
       or dl,30h
       mov ah,02h
       int 21h
       loop disp
       mov dl,'B'
       mov ah,2
       int 21h
       ret
change4 endp
code  ends
      end 


change5

public change5
data segment
binary dw 7fffh,50h
       n=($-binary)/2
buf  db 7 dup(0)
info1 db 0ah,0dh,'this is the change from hexnumber to decimal:$'
info2 db 0ah,0dh,'the number is:7fffh,50h$',0ah,0dh
info3 db 0ah,0dh,'the result is:$'
data ends
stack segment stack
      db 200 dup(0)
stack ends     
code segment
     assume cs:code,ds:data,ss:stack
f2to10 proc near
      push bx
      push dx
      push si
      push cx
disp  macro m
      lea dx,m
      mov ah,9
      int 21h
      endm
      lea si,buf
      or  ax,ax
      jns plus
      neg ax
      mov [si],byte ptr '-'
      inc si
plus: mov bx,10
      mov cx,0
lop1: mov dx,0
      div bx
      push dx
      inc cx
      or ax,ax
      jne lop1
lop2: pop ax
      cmp al,10
      jb l1
      add al,7
l1:   add al,30h
      mov [si],al
      inc si
      dec cx
      jne lop2
      mov [si],byte ptr 'D'
      inc si
      mov [si],byte ptr '$'
      disp  info3
      disp buf
      pop cx
      pop si
      pop dx
      pop bx
      ret
f2to10 endp
change5 proc far
start: mov ax,data
       mov ds,ax
       disp info1
       disp info2
       mov cx,n
       lea di,binary
lopa:  mov ax,[di]
       call f2to10
       add di,2
       loop lopa
       ret
change5 endp
code ends
      end 

函数功能:
主函数(start)用于显示欢迎界面;子函数main用于调用各功能模块;change1用于将小写字母转换成大写字母;change2用于将大写字母转换成小写;change3用于将2进制转换成16进制;change4用于将16进制转换成2进制;change5用于16进制转换10进制并调用f2to10并显示;f2to10用于将2进制转换成10进制。
输出字符串时,使用宏disp实现。宏disp通过DOS中断实现输出。

参数传递:
主函数与子函数之间函数传递采用了寄存器法和约定单元法,寄存器采用了BX,SI,DI,约定单元有info1,info2,bufa,bufb,buf,info0,info3。

中断向量:
本题用到了10H、21H函数库的函数调用,入口地址为0:40h ,0:84h。
10H号函数库:CS=410h+2h=42h , IP=410h=40h 。中断向量为0:40H。
21H号函数库:CS=421h+2h=86h , IP=421h=84h 。中断向量为0:84h。

你可能感兴趣的:(汇编语言学习)