鱼C论坛

 找回密码
 立即注册
查看: 2821|回复: 0

[汇编作业] 显示数据段中的数

[复制链接]
发表于 2017-6-21 10:39:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. ;----显示数据段中的数----
  2. assume cs:code,ds:data,ss:stack

  3. data segment
  4.         dw 1230,34666,1,2,8,89,1,0ffffh
  5.         db 40h dup(?)
  6. data ends

  7. stack segment
  8.         dw 10h dup(0)
  9. stack ends

  10. code segment
  11.         start:
  12.         mov ax,data
  13.         mov ds,ax
  14.         mov ax,stack
  15.         mov ss,ax
  16.         mov sp,10h
  17.        
  18.         call dtoc
  19.        
  20.        
  21.                  ;------准备显示参数------------
  22.         mov ax,10h                        ;第一个处理好的数据的地址
  23.         add si,ax
  24.         mov cl,02h          ;显示的颜色。这里是绿色
  25.         mov dh,16
  26.         mov dl,59           ;注释:dl只能取单数,1表示从第一列显示,3表示从第二列显示...159表示从第80
  27.                             ;列开始显示即是(dl+1)/2,取偶数将出现乱码:-)
  28.         call show_str
  29.        
  30.        
  31.         mov ax,4c00h
  32.         int 21h
  33.        
  34.         dtoc:
  35.                
  36.                 push si
  37.                 push ax
  38.                 push cx
  39.                
  40.                 sub si,si                ;读数据段数据的offset address
  41.                 mov di,10h                ;写第一个数据的offset address
  42.                                
  43.                 rec:
  44.                 mov ax,[si]                ;取一个数据
  45.                 mov cx,ax       ;放入cx
  46.                 sub cx,0ffffh        ;若取到的字是0ffffh,结束
  47.                 jcxz endd       ;
  48.                
  49.                
  50.                 call trans
  51.                
  52.                 add si,2                ;下一个数据位置
  53.                 jmp short rec
  54.                 endd:           ;
  55.                 inc di          ;
  56.                 inc di          ;
  57.                 sub ax,ax       ;
  58.                 mov [di],ax                ;;最后一个数据后放一个结束标志0.供show_str函数用
  59.                 pop cx
  60.                 pop ax
  61.                 pop si
  62.         ret
  63.                  
  64.         trans:   ;---------转换一个数并放入内存中----------------------
  65.                    push ax
  66.                    push bx
  67.                    push cx
  68.                    push dx
  69.                    push si
  70.                   
  71.                   
  72.                    rmod:  ;--------循环取余数------------------------
  73.                         ;DIVDW 的参数:(ax)=dword型数据的低16位,(dx)=dword型数据的高16位,
  74.                         ;      (cx)=除数
  75.                         ;返回:(dx)=结果的高16位,(ax)=结果的低16位。(cx)=余数
  76.                    mov cx,0ah                ;除数传传入CX,低16位被除数在AX中
  77.                    sub dx,dx                ;被除数高位是0
  78.                   
  79.                    call divdw                 ;做除法  .商在Ax,余数在cx中
  80.                   
  81.                    add cx,30h                ;转换成ascii吗
  82.                    push cx                        ;入栈
  83.                    inc bx                        ;计数入栈数据的数量
  84.                   
  85.                    mov cx,ax                ;商放CX中等待检查
  86.                    jcxz endt                ;商为零跳出
  87.                                      
  88.                    jmp short rmod
  89.                   endt:
  90.                         mov cx,bx
  91.                         spop:
  92.                         pop [di]                ;顺序将数据出栈,并存入内存
  93.                         add di,2
  94.                         loop spop
  95.                        
  96.                         mov ax,2020h        ;每个数据中间插入2个空格
  97.                         mov [di],ax
  98.                         inc di
  99.                         inc di                        ;下一个存储数据位置
  100.                        
  101.                   pop si
  102.                   pop dx
  103.                   pop cx
  104.                   pop bx
  105.                   pop ax
  106.         ret
  107.                   
  108.         ;注解:DIV CX;--商在AX中,余数在DX中 ;   DIV BL;商在AL中,余数在AH中           
  109.         divdw:
  110.                 push si
  111.                 push ds
  112.                 push bx
  113.                
  114.                 sub si,si       ;数据暂存位置
  115.                 mov bx,ax                ;被除数低16位存放到bx
  116.                 mov ax,dx                ;高16位放入ax,准备除法
  117.                 sub dx,dx                ;高16位清零
  118.                 div cx                        ;H/N
  119.                 push ax                        ;高16位除法后的商 入栈
  120.                                  
  121.                 mov ax,bx                 ;低16位数放ax中。因为余数在DX中,会直接参与低位除法的运算,
  122.                                
  123.                 div cx                         ;相当于[(余数 X 10000H)+ 低16位数] ÷ 除数。注:----商在AX中
  124.                
  125.                 mov cx,dx                 ;余数存入cx--over
  126.                 pop dx           ;高位商存入dx
  127.                
  128.                 pop bx       
  129.                 pop ds
  130.                 pop si
  131.         ret
  132.        
  133.         ;名称:show_str。功能:指定的位置,显示一个用0结束的字符串
  134.         ;参数:(dh)=行号(取值范围0~24),(dl)=列号(取值范围0~79),(cl)=颜色,
  135.         ;       ds:si指向字符串首地址
  136.         show_str:
  137.                 push si
  138.                 push di
  139.                 push es
  140.                 push ax
  141.                 push bx
  142.                 push cx
  143.                 push dx
  144.                
  145.                 call cls
  146.                
  147.                 mov ax,0b800h      
  148.         mov es,ax           ;显示缓存段地址
  149.             dec dh
  150.             dec dl
  151.             mov al,0a0h
  152.             mul dh              ;显示的行地址
  153.             sub dh,dh
  154.             add ax,dx           ;行+列的地址
  155.             mov di,ax           ;显示线性地址放di
  156.                 mov bl,cl           ;因为后面要检测cx值,所以将cl暂存bl
  157.                    
  158.                 s0:
  159.                 mov cl,bl
  160.                 mov ch,ds:[si]       ;要显示的字符
  161.                 mov es:[di],ch       ;显示的字符放对应显示缓存的低字节
  162.                 mov es:[di+1],cl     ;显示的属性放对应显示缓存的高字节
  163.                 inc        si
  164.                 inc si                             ;下一个要显示的字符
  165.                 add di,2             ;下一个要显示缓存地址
  166.                
  167.                 sub cl,cl
  168.                 jcxz ok
  169.                 jmp short s0
  170.                 ok: pop dx
  171.                         pop cx
  172.                         pop bx
  173.                         pop ax
  174.                         pop es
  175.                         pop di
  176.                         pop si
  177.         ret
  178.                
  179.                   
  180.         cls:
  181.                    push ax
  182.                    push cx
  183.                    push es
  184.                    push si
  185.                    mov cx,3840
  186.                    mov ax,0b800h
  187.                    mov es,ax
  188.                    sub si,si
  189.                    sc:mov ax,3000h
  190.                      mov es:[si],ax
  191.                      inc si
  192.                          inc si
  193.                          loop sc
  194.                    pop si
  195.                    pop es
  196.                    pop cx
  197.                    pop ax
  198.         ret
  199.        
  200.        
  201. code ends
  202. end start
复制代码


调试拍错真 的难,做好注释很重要

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 09:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表