在以下8086汇编代码中无法使用数组显示负数 [英] Unable to display negative numbers using array in the following 8086 assembly code

查看:123
本文介绍了在以下8086汇编代码中无法使用数组显示负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做什么

我正在尝试使用数组打印一系列负数和正数.

I am trying to print a series of negative and positive numbers using array.

出了什么问题

我无法打印负数.

输入:

2,-3,-4,5,2,9

输出:

2-,529

我的8086汇编代码:

.model small
.stack 100h
.data                   
     elements db 2,-3,-4,5,2,9,'#'
.code

     mov ax, @data
     mov ds, ax  

     mov al, 03h
     mov ah, 0
     int 10h    
     mov si, 0
     ;lea si, elements

dis:

     cmp elements[si], '#'
     je exit

     mov dl, elements[si]
     add dl, 48
     mov ah, 02h
     int 21h     
     inc si 
     loop dis
exit:    
     mov ah, 04ch
     int 21h    
end

注意:我是组装的完整入门者.

推荐答案

您的程序有2个主要问题:

Your program has 2 major problems:

  1. 您正在使用loop指令,而无需事先初始化计数器寄存器CX!幸运的是,您的代码在CX寄存器的顶部还有一个额外的终止条件,开始时可能为零.然后以为您甚至不需要此程序中的此指令.

  1. You're using the loop instruction without having initialized the counter register CX beforehand! Luckily your code has an extra terminating condition on top of the CX register probably being zero to start with. And then to think that you don't even need this instruction in this program.

您永远不会测试从数组中读取的数字是正数还是负数.

You never test if the number that you read from the array is positive or negative.

这是一个完整的解决方案,带有其他重要的注释:

Here's a complete solution with additional important comments:

  xor  si, si          ;Same as "mov si,0"
dis:
  mov  bl, elements[si]
  cmp  bl, '#'
  je   exit
  test bl, bl          ;Same as "cmp bl, 0"
  jns  IsPositive
  mov  dl, "-"         ;First display a minus sign
  mov  ah, 02h
  int  21h
  neg  bl              ;Turn the negative number into a positive number
IsPositive:
  mov  dl, "0"
  add  dl, bl          ;Change the value [0,9] into character ["0","9"]
  mov  ah, 02h
  int  21h     
  inc  si 
  jmp  dis             ;Unconditionally jump back. Don't use "loop"
exit:    
  mov  ax, 4C00h       ;Always use the full AX, the API defines it that way!
  int  21h

这篇关于在以下8086汇编代码中无法使用数组显示负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆