鱼C论坛

 找回密码
 立即注册
查看: 5868|回复: 1

求助:汇编输出99乘法表

[复制链接]
发表于 2020-12-6 17:13:41 | 显示全部楼层 |阅读模式
10鱼币
汇编编写输出99乘法表的程序:输出是无限循环,而且是乱码,求各位大佬帮忙看一下是哪里有问题,万分感谢!
  1. assume cs:code,ds:data,ss:stack
  2. data segment
  3.     num1 dw 1;乘数1
  4.     num2 dw 1;乘数2
  5.     num3 dw 1;乘积
  6.     char1 dw '*;乘号
  7.     char2 dw '=;等号
  8.     char3 dw 0ah,';空格
  9.     char4 dw 0dh,';回车
  10.     char0 dw 0,0,0,0,';储存数字转换成的字符串
  11. data ends

  12. stack segment
  13.     dw 8 dup(0)
  14. stack ends

  15. code segment
  16. start:
  17.     mov ax,data
  18.     mov ds,ax;初始化
  19.     mov di,9;记录外层循环次数
  20. s1:
  21.     mov cx,ds:[num1];内层循环次数由num1决定,如2*1这一行内部循环2次
  22. s2:;进入内层循环
  23.     call instack;跳转到instack,用于将数字转换为字符输出
  24.     mov dx,[num1]
  25.     mov al,dl
  26.     mov ah,0;设置al为乘数1
  27.     mov dx,[num2]
  28.     mul dl;结果存入ax中
  29.     mov [num3],ax;将结果存入num3中
  30.     add [num3],30h;将num3转换为字符
  31.     mov ax,[num3]
  32.     mov [char0+4],ax;存入放字符的数据段中
  33.     call output;跳转到输出
  34.     inc [num2];乘数2自增1
  35.     loop s2;内层循环

  36.     lea dx,[char4]
  37.     mov ah,9
  38.     int 21h;一次内层循环完毕后,输出换行
  39.     mov [num2],1;乘数2变回1
  40.     dec di;记录外层循环的数字-1
  41.     mov cx,di;外层循环次数
  42.     inc [num1];乘数1自增1
  43.     loop s1;外层循环

  44.     mov ah,4ch
  45.         int 21h

  46. instack:
  47.     push [num1];将乘数1入栈
  48.     mov si,0
  49.     add ss:[si],30h;乘数1转换为字符
  50.     pop [char0+0];将乘数1字符送入储存字符的数据段中
  51.     push [num2];乘数2同理
  52.     mov si,0
  53.     add ss:[si],30h
  54.     pop [char0+2]
  55.     ret;跳转回主程序

  56. output:
  57.     mov si,0
  58.     lea dx,[char0+si]
  59.     mov ah,9
  60.     int 21h;输出乘数1的字符
  61.     lea dx,char1
  62.     mov ah,9
  63.     int 21h;输出乘号
  64.     mov si,2
  65.     lea dx,[char0+si]
  66.     mov ah,9
  67.     int 21h;输出乘数2的字符
  68.     lea dx,char2
  69.     mov ah,9
  70.     int 21h;输出等号
  71.     mov si,4
  72.     lea dx,[char0+si]
  73.     mov ah,9
  74.     int 21h;输出乘数2的字符
  75.     lea dx,char3
  76.     mov ah,9
  77.     int 21h;输出空格
  78.     ret;返回


  79. code ends
  80. end start
复制代码

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

使用道具 举报

发表于 2020-12-6 21:48:09 | 显示全部楼层
assume cs:code,ds:data,ss:stack


data segment
    db 100 dup(0)
data ends

stack segment
    dw 100 dup(0)
stack ends

code segment
    ;dl*dh=ax
    show:
    push si
    push dx
    push ax
        
        ;被乘数
    mov si,0
    add dl,48               
    mov [si],dl
        
        ;乘号
    inc si
    mov byte ptr [si],'*'
        
        ;乘数
    inc si
    add dh,48
    mov [si],dh
        
        ;等号
    inc si
    mov byte ptr [si],'='
        
        ;前面 mul dl 的积 一直是存放在 ax 中,现在拿ax 除以10 ,如果商为0 说明只有一位数,直接转换成对应的 ascii码 存入到内存中
    inc si
    mov dl,10
    div dl
        
        ;判断al商是否为0
    cmp al,0
    je zero
   
        ;如果这里被执行,说明商有2位数,这里只需要把个位转换就下了,十位ah ,永远都会被转换
    add al,48
    mov byte ptr [si],al
    inc si
   
        ;al商为零就跳转到这里来
    zero:
    add ah,48
    mov byte ptr [si],ah
   
        ;结果后面加个空格方便好看
    inc si
    mov word ptr [si],'  '
    add si,2
    mov byte ptr [si],'


   
        ;打印
    mov dx,0
    mov ah,9
    int 21h
   
    pop ax
    pop dx
    pop si
        
    ret  ;返回到94行 inc dh
   
    start:
                mov ax,data
                mov ds,ax
               
                mov ax,stack
                mov ss,ax
                mov sp,200
               
                mov cx,9
                mov dl,1  ;dl*dh-->dl*al
  s0:
    push cx
    mov dh,1    ;dh=1
   
    mov cl,dl        ;cl=1
    mov ch,0    ;ch=0

    s1:
    mov al,dh        ;al=1
   
    mul dl      ;dh*dl
   
    call show
    inc dh      ;dh++
   
    loop s1
   
    push dx
    mov ah,2h
    mov dl,0ah
    int 21h
    mov ah,2h
    mov dl,0dh
    int 21h
    pop dx
   
   
   
    inc dl  ;dl++
    pop cx
  loop s0
   
    mov ax,4c00h
    int 21h
   
code ends
end start
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-8 15:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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