鱼C论坛

 找回密码
 立即注册
查看: 4086|回复: 10

[已解决]为什么我的输出字符串回事乱码

[复制链接]
发表于 2017-4-5 18:56:48 | 显示全部楼层 |阅读模式

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

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

x
如图,如题。望不吝赐教。
最佳答案
2017-4-6 16:41:39

不要用bp,sp寄存器

不要在一个循环中做太多事

我看不懂你要干嘛
我帮你改写了一下
  1. assume cs:code, ds:data

  2. extrn write:far

  3. data segment
  4.         str1 db 0dh, 0ah, "There are  $"
  5.         str2 db "  data equal to zero in the array! $"
  6.         str3 db "  data greater than zero in the array! $"
  7.         str4 db "  data less than zero in the array! $"
  8.         str5 db 0dh, 0ah, "The max is $"
  9.         str6 db 0dh, 0ah, "The min is $"
  10.        
  11.         zero_count dw 0 ;等于0的计数器
  12.         greater_count dw 0 ;大于0的计数器
  13.         less_count dw 0 ;小于0的计数器
  14.        
  15.         max dw 0 ;最大值
  16.         min dw 0 ;最小值

  17.         array dw 40, 12, 0, -2, 23, -12, 0, 2, -10, 100, 0, -100
  18.         len equ ($ - array) / 2
  19. data ends


  20. code    segment

  21. outstr macro msg
  22.         lea dx, msg
  23.         mov ah, 9
  24.         int 21h ;调用9号功能输出字符
  25. endm
  26.         
  27. disp macro integer
  28.         mov ax, integer
  29.         stc
  30.         call far ptr write
  31. endm ;这一段调用子程序write用来输出

  32. start:
  33.         mov ax, data
  34.         mov ds, ax
  35.        
  36.        
  37. ;查找等于0的个数
  38. find_zero:
  39.         lea bx, array
  40.         mov cx, len
  41.         mov zero_count, 0
  42. s:
  43.         cmp word ptr[bx], 0
  44.         jne next
  45.        
  46.         inc zero_count
  47. next:
  48.         add bx, 2
  49.         loop s


  50. ;查找大于0的个数
  51. find_greater_zero:
  52.         lea bx, array
  53.         mov cx, len
  54.         mov greater_count, 0
  55. s1:
  56.         cmp word ptr[bx], 0
  57.         jng next1
  58.        
  59.         inc greater_count
  60. next1:
  61.         add bx, 2
  62.         loop s1

  63. ;查找小于0的个数
  64. find_less_zero:
  65.         lea bx, array
  66.         mov cx, len
  67.         mov less_count, 0
  68. s2:
  69.         cmp word ptr[bx], 0
  70.         jnl next2
  71.        
  72.         inc less_count
  73. next2:
  74.         add bx, 2
  75.         loop s2
  76.        
  77. ;查找最大值
  78. find_max:
  79.         lea bx, array
  80.         mov cx, len
  81.         mov ax, [bx]
  82.         mov max, ax
  83. s3:
  84.         mov ax, [bx]
  85.         cmp max, ax
  86.         jg next3
  87.        
  88.         mov max, ax
  89. next3:
  90.         add bx, 2
  91.         loop s3
  92.        
  93. ;查找最小值
  94. find_min:
  95.         lea bx, array
  96.         mov cx, len
  97.         mov ax, [bx]
  98.         mov min, ax
  99. s4:
  100.         mov ax, [bx]
  101.         cmp min, ax
  102.         jl next4
  103.        
  104.         mov min, ax
  105. next4:
  106.         add bx, 2
  107.         loop s4
  108.        
  109.        
  110.         outstr str1
  111.         disp zero_count                ;输出等于0的数据的个数
  112.         outstr str2
  113.         outstr str1
  114.         disp greater_count        ;输出大于0的数据的个数
  115.         outstr str3
  116.         outstr str1
  117.         disp less_count                ;输出小于0的数据的个数
  118.         outstr str4
  119.         outstr str5
  120.         disp max        ;输出最大值
  121.         outstr str6
  122.         disp min        ;输出最小值

  123.         mov ax, 4c00h
  124.         int 21h
  125. code ends

  126. end start
复制代码

出现的问题

出现的问题

代码行

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

使用道具 举报

发表于 2017-4-5 19:18:25 | 显示全部楼层
把代码贴上来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-5 23:45:00 | 显示全部楼层

extrn  write:far
data    segment
str1     db  0dh,0ah,"There are  $"
str2     db  "  data equal to zero in the array! $"
str3     db  "  data greater than zero in the array! $"
str4     db  "  data less than zero in the array! $"
str5     db  0dh,0ah,"The max is $"
str6     db  0dh,0ah,"The min is $"
sum      dw  0
array    dw  40,12,0,-2,23,-12,0,2,-10,100,0,-100
len      equ  ($-array)/2
data    ends
code    segment
assume cs:code,ds:data
outstr   macro  msg
        lea dx,msg
        mov ah,9
        int  21h
        endm
disp     macro  integer
        mov ax,integer
        stc
        call far ptr write
        endm
start:    mov ax,data
        mov ds,ax
        mov cx,len
        mov sum,0                   ;sum为等于0数的计数器
        mov si,0                   ;si为大于0数的计数器
        mov di,0                   ;di为小于0数的计数器
      
        lea bx,array
        mov bp,[bx]                   ;bp为最大值
        mov sp,[bx]                    ;sp为最小值

      

again:   disp [bx]
        mov dl,20h                   ;单字符输出空格
        mov ah,2
        int  21h
        cmp word ptr [bx],0
        jnz  nz
        inc  sum
        cmp word ptr [bx],bp
        jg greater
        cmp word ptr [bx],sp
        jl less
        jmp  next
nz:      jg  plus
        inc  di
        cmp word ptr [bx],bp
        jg greater
        cmp word ptr [bx],sp
        jl less
        jmp  next
plus:    inc  si
         cmp word ptr [bx],bp
        jg greater
        cmp word ptr [bx],sp
        jl less
greater:  mov bp,[bx]
         jmp next
less:     mov sp,[bx]
         jmp next        
next:    inc  bx
        inc  bx
        loop  again
        
        outstr  str1
        disp sum
        outstr  str2
        outstr  str1
        disp si
        outstr  str3
        outstr  str1
        disp di
        outstr  str4
        outstr  str5
        disp bp
        outstr  str6
        disp sp
        

     
        mov ax,4c00h
        int  21h
code    ends
        end  start
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-6 11:15:11 | 显示全部楼层
write 是什么?
无标题.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-6 11:38:22 | 显示全部楼层

是一个子程序,我把它的obj传上来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-6 11:42:47 | 显示全部楼层

放在一起就能可以用了

DISPINT.zip

316 Bytes, 下载次数: 1

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

使用道具 举报

发表于 2017-4-6 12:34:57 | 显示全部楼层
我重新调整了一下

请添加一下注释

  1. assume cs:code, ds:data

  2. extrn write:far

  3. data segment
  4.         str1 db 0dh, 0ah, "There are  $"
  5.         str2 db "  data equal to zero in the array! $"
  6.         str3 db "  data greater than zero in the array! $"
  7.         str4 db "  data less than zero in the array! $"
  8.         str5 db 0dh, 0ah, "The max is $"
  9.         str6 db 0dh, 0ah, "The min is $"
  10.         sum dw 0
  11.        
  12.         array dw 40, 12, 0, -2, 23, -12, 0, 2, -10, 100, 0, -100
  13.         len equ ($ - array) / 2
  14. data ends

  15. code    segment

  16. outstr macro msg
  17.         lea dx, msg
  18.         mov ah, 9
  19.         int  21h
  20. endm
  21.        
  22. disp macro integer
  23.         mov ax, integer
  24.         stc
  25.         call far ptr write
  26. endm

  27. start:
  28.         mov ax, data
  29.         mov ds, ax
  30.         mov cx, len
  31.         mov sum, 0 ;sum为等于0数的计数器
  32.         mov si, 0 ;si为大于0数的计数器
  33.         mov di, 0 ;di为小于0数的计数器

  34.         lea bx, array
  35.         mov bp, [bx] ;bp为最大值
  36.         mov sp, [bx] ;sp为最小值

  37. again:
  38.         disp [bx]
  39.         mov dl, 20h ;单字符输出空格
  40.         mov ah, 2
  41.         int 21h

  42.         cmp word ptr [bx], 0
  43.         jnz nz

  44.         inc sum
  45.         cmp word ptr [bx], bp
  46.         jg greater

  47.         cmp word ptr [bx], sp
  48.         jl less
  49.         jmp next

  50. nz:
  51.         jg  plus
  52.        
  53.         inc  di
  54.         cmp word ptr [bx],bp
  55.         jg greater
  56.        
  57.         cmp word ptr [bx],sp
  58.         jl less
  59.         jmp  next
  60.        
  61. plus:
  62.         inc si
  63.         cmp word ptr [bx], bp
  64.         jg greater
  65.        
  66.         cmp word ptr [bx], sp
  67.         jl less
  68.        
  69. greater:
  70.         mov bp, [bx]
  71.         jmp next
  72.        
  73. less:
  74.         mov sp, [bx]
  75.         jmp next
  76.         
  77. next:
  78.         inc bx
  79.         inc bx
  80.         loop again
  81.        
  82.         outstr str1
  83.         disp sum
  84.         outstr str2
  85.         outstr str1
  86.         disp si
  87.         outstr str3
  88.         outstr str1
  89.         disp di
  90.         outstr str4
  91.         outstr str5
  92.         disp bp
  93.         outstr str6
  94.         disp sp
  95.         
  96.         mov ax, 4c00h
  97.         int 21h
  98. code ends

  99. end start
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-6 13:43:11 | 显示全部楼层
人造人 发表于 2017-4-6 12:34
我重新调整了一下

请添加一下注释
  1. assume cs:code, ds:data

  2. extrn write:far

  3. data segment
  4.         str1 db 0dh, 0ah, "There are  $"
  5.         str2 db "  data equal to zero in the array! $"
  6.         str3 db "  data greater than zero in the array! $"
  7.         str4 db "  data less than zero in the array! $"
  8.         str5 db 0dh, 0ah, "The max is $"
  9.         str6 db 0dh, 0ah, "The min is $"
  10.         sum dw 0
  11.         
  12.         array dw 40, 12, 0, -2, 23, -12, 0, 2, -10, 100, 0, -100
  13.         len equ ($ - array) / 2
  14. data ends

  15. code    segment

  16. outstr macro msg
  17.         lea dx, msg
  18.         mov ah, 9
  19.         int  21h
  20. endm    ;这一段调用9号功能输出字符
  21.         
  22. disp macro integer
  23.         mov ax, integer
  24.         stc
  25.         call far ptr write
  26. endm   ;这一段调用子程序write用来输出

  27. start:
  28.         mov ax, data
  29.         mov ds, ax
  30.         mov cx, len
  31.         mov sum, 0 ;sum为等于0数的计数器
  32.         mov si, 0 ;si为大于0数的计数器
  33.         mov di, 0 ;di为小于0数的计数器

  34.         lea bx, array ;获取array的首地址给bx寄存器
  35.         mov bp, [bx] ;bp为最大值
  36.         mov sp, [bx] ;sp为最小值

  37. again:
  38.         disp [bx]
  39.         mov dl, 20h ;单字符输出空格
  40.         mov ah, 2
  41.         int 21h     ;输出一个数组内的数据并输出一个空格
  42.                
  43.         cmp word ptr [bx], 0   ;bx里的数据跟0做对比
  44.         jnz nz    ;数据不等于0,则跳转到标号nz,等于0则不跳转

  45.         inc sum    ;数据等于0,sum加1,记下等于0的数据的个数
  46.         cmp word ptr [bx], bp     ;数据与当前最大值比较
  47.         jg greater        ;数据比当前最大值大,跳转到greater,否则不跳转

  48.         cmp word ptr [bx], sp   ;数据与当前最小值比较
  49.         jl less                   ;数据比当前最小值小,跳转到less,否则不跳转
  50.         jmp next

  51. nz:
  52.         jg  plus       ;数据比0大,跳转到pius,否则不跳转
  53.         
  54.         inc  di       ;记下小于0的数据的个数
  55.         cmp word ptr [bx],bp
  56.         jg greater
  57.         
  58.         cmp word ptr [bx],sp
  59.         jl less
  60.         jmp  next            ;跳转到next
  61.         
  62. plus:
  63.         inc si      ;记下比0大的数据的个数
  64.         cmp word ptr [bx], bp
  65.         jg greater
  66.         
  67.         cmp word ptr [bx], sp
  68.         jl less
  69.         
  70. greater:
  71.         mov bp, [bx]         ;将比当前最大值大的bx指向的数据作为新的最大值
  72.         jmp next
  73.         
  74. less:
  75.         mov sp, [bx]     ;把比当前最小值小的bx指向的数据作为新的最小值
  76.         jmp next
  77.         
  78. next:
  79.         inc bx   
  80.         inc bx            ;bx+2,指向下一个字数据
  81.         loop again    ;从again标号再次开始
  82.         
  83.         outstr str1        ;输出字符串
  84.         disp sum            ;输出等于0的数据的个数
  85.         outstr str2
  86.         outstr str1
  87.         disp si             ;输出大于0的数据的个数
  88.         outstr str3
  89.         outstr str1
  90.         disp di         ;输出小于0的数据的个数
  91.         outstr str4
  92.         outstr str5
  93.         disp bp          ;输出最大值
  94.         outstr str6
  95.         disp sp          ;输出最小值
  96.         
  97.         mov ax, 4c00h
  98.         int 21h
  99. code ends

  100. end start
复制代码

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

使用道具 举报

发表于 2017-4-6 16:41:39 | 显示全部楼层    本楼为最佳答案   

不要用bp,sp寄存器

不要在一个循环中做太多事

我看不懂你要干嘛
我帮你改写了一下
  1. assume cs:code, ds:data

  2. extrn write:far

  3. data segment
  4.         str1 db 0dh, 0ah, "There are  $"
  5.         str2 db "  data equal to zero in the array! $"
  6.         str3 db "  data greater than zero in the array! $"
  7.         str4 db "  data less than zero in the array! $"
  8.         str5 db 0dh, 0ah, "The max is $"
  9.         str6 db 0dh, 0ah, "The min is $"
  10.        
  11.         zero_count dw 0 ;等于0的计数器
  12.         greater_count dw 0 ;大于0的计数器
  13.         less_count dw 0 ;小于0的计数器
  14.        
  15.         max dw 0 ;最大值
  16.         min dw 0 ;最小值

  17.         array dw 40, 12, 0, -2, 23, -12, 0, 2, -10, 100, 0, -100
  18.         len equ ($ - array) / 2
  19. data ends


  20. code    segment

  21. outstr macro msg
  22.         lea dx, msg
  23.         mov ah, 9
  24.         int 21h ;调用9号功能输出字符
  25. endm
  26.         
  27. disp macro integer
  28.         mov ax, integer
  29.         stc
  30.         call far ptr write
  31. endm ;这一段调用子程序write用来输出

  32. start:
  33.         mov ax, data
  34.         mov ds, ax
  35.        
  36.        
  37. ;查找等于0的个数
  38. find_zero:
  39.         lea bx, array
  40.         mov cx, len
  41.         mov zero_count, 0
  42. s:
  43.         cmp word ptr[bx], 0
  44.         jne next
  45.        
  46.         inc zero_count
  47. next:
  48.         add bx, 2
  49.         loop s


  50. ;查找大于0的个数
  51. find_greater_zero:
  52.         lea bx, array
  53.         mov cx, len
  54.         mov greater_count, 0
  55. s1:
  56.         cmp word ptr[bx], 0
  57.         jng next1
  58.        
  59.         inc greater_count
  60. next1:
  61.         add bx, 2
  62.         loop s1

  63. ;查找小于0的个数
  64. find_less_zero:
  65.         lea bx, array
  66.         mov cx, len
  67.         mov less_count, 0
  68. s2:
  69.         cmp word ptr[bx], 0
  70.         jnl next2
  71.        
  72.         inc less_count
  73. next2:
  74.         add bx, 2
  75.         loop s2
  76.        
  77. ;查找最大值
  78. find_max:
  79.         lea bx, array
  80.         mov cx, len
  81.         mov ax, [bx]
  82.         mov max, ax
  83. s3:
  84.         mov ax, [bx]
  85.         cmp max, ax
  86.         jg next3
  87.        
  88.         mov max, ax
  89. next3:
  90.         add bx, 2
  91.         loop s3
  92.        
  93. ;查找最小值
  94. find_min:
  95.         lea bx, array
  96.         mov cx, len
  97.         mov ax, [bx]
  98.         mov min, ax
  99. s4:
  100.         mov ax, [bx]
  101.         cmp min, ax
  102.         jl next4
  103.        
  104.         mov min, ax
  105. next4:
  106.         add bx, 2
  107.         loop s4
  108.        
  109.        
  110.         outstr str1
  111.         disp zero_count                ;输出等于0的数据的个数
  112.         outstr str2
  113.         outstr str1
  114.         disp greater_count        ;输出大于0的数据的个数
  115.         outstr str3
  116.         outstr str1
  117.         disp less_count                ;输出小于0的数据的个数
  118.         outstr str4
  119.         outstr str5
  120.         disp max        ;输出最大值
  121.         outstr str6
  122.         disp min        ;输出最小值

  123.         mov ax, 4c00h
  124.         int 21h
  125. code ends

  126. end start
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
兰陵月 + 5 + 5 + 3 热爱鱼C^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

 楼主| 发表于 2017-4-6 18:48:21 | 显示全部楼层
人造人 发表于 2017-4-6 16:41
不要用bp,sp寄存器

不要在一个循环中做太多事

可以了,谢谢啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-6 18:52:57 | 显示全部楼层
人造人 发表于 2017-4-6 16:41
不要用bp,sp寄存器

不要在一个循环中做太多事

给了我很大的启示,我刚学汇编没多久,写程序总是很乱,感觉。你这个很清晰。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-2 19:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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