+ -
当前位置:首页 → 问答吧 → 汇编新手救助

汇编新手救助

时间:2011-11-17

来源:互联网

谁能帮我看看错在那里啊?
我想要的输出是
How many numbers would you like to add: 5
Enter a number: 10
Enter a number: 25
Enter a number: 10
Enter a number: 25
Enter a number: 50
The sum of the numbers is: 120
这样的,但这个程序怎么就2个数相加啊?
.model small
.stack 100h
.data 
  indata dw 2 dup(?)
  outdata dw 1 dup(?)
  str1 db 'How many numbers would you like to add:$'
  notein1 db 'Enter a number:$'
   
  noteout db 'The sum of the numbers is:$'
  notewarn db 'warning:wrong char!$'
  noteagain db 'please input again:$'

.code 
   
main proc 
   
 push ds
 sub ax,ax
 push ax
 mov ax,@data
 mov ds,ax

lea dx,str1
mov ah,09h
int 21h
mov ah,01h
int 21h
sub al,30h
xor ah,ah
mov cx,ax
xor bx,bx
mov dl,0ah
mov ah,02h
int 21h

 mov es,ax
lea dx, notein1
mov ah,9h
  int 21h
 call crlf
 call deci_input
 mov indata,bx
 call crlf
lea dx,notein1
mov ah,9h
  int 21h
 call crlf
 call deci_input
 mov indata+2,bx
 call crlf
 mov ax,bx
 add ax,indata
 mov outdata,ax
lea dx,noteout
mov ah,9h
  int 21h
 call crlf
 call deci_output
  ret
main endp

deci_input proc near
 mov bx,0
  newchar:mov ah,1
 int 21h
 cmp al,0dh
 jz exit
 sub al,30h
 jl noteerr
 cmp al,9d
 jg noteerr
 cbw
 xchg ax,bx
 mov cx,10d
 mul cx
 xchg ax,bx
 add bx,ax
 jmp newchar
  noteerr:call crlf
 lea dx, notewarn
mov ah,9h
  int 21h
 call crlf
 lea dx, noteagain
mov ah,9h
  int 21h
 jmp newchar
  exit: ret
deci_input endp

deci_output proc near
 mov bx,outdata
 mov cl,100d
 mov ax,bx
 div cl
 mov bl,ah
 mov dl,al
 add dl,30h
 call dispchar
 mov ax,0
 mov al,bl
 cbw
 mov cl,10d
 div cl
 mov bl,ah
 mov dl,al
 add dl,30h
 call dispchar
 mov dl,bl
 add dl,30h
 call dispchar
 ret
deci_output endp

crlf proc near
 mov dl,0ah
 call dispchar
 mov dl,0dh
 call dispchar
 ret
crlf endp

dispchar proc near
 mov ah,2h
 int 21h
 ret
dispchar endp
  end main
高手们帮帮我吧。。。。

作者: cmychq   发布时间: 2011-11-17

这程序是你自己写的吗?你的程序里就没有根据输入的数据个数来进行循环读取数值,只是两次调用了 deci_input 来读入了两个数据进行了处理。另外,最后的 ret 应该用 retf 指令,或者将 main 子程定义为 far ,否则,程序会陷入死循环,因为 ret 会被解释为 retn 接着转去了程序的开始处。

作者: zara   发布时间: 2011-11-17

引用楼主 cmychq 的回复:
谁能帮我看看错在那里啊?
我想要的输出是
How many numbers would you like to add: 5
Enter a number: 10
The sum of the numbers is: 120
这样……

学汇编,要会用划整为零、逐步提高的办法来学习,这有利于积累学习成果,如你可以按以下步骤推进学习(每一步都可以做成可输出的程序来检验)
(1)如何显示一个字符(这个你的程序有了)
(2)如何显示一个串(这个你的程序也有了)
(3)如何将bx寄存器中的二进制无符号整数,变换为十进制ASCII串,并显示出来
  这个问题你的程序没有很好地解决!(算法不科学,要以10为基,碾转除),这个要练习
(4)如何将定义在数据段中的 var dw 1234 变换为 s db '1234$' 并显示出来(按十进制转换)
(5)如何将定义在数据段中的 array dw 1234,4567,2345,3344 各项分别变换为对应的ASCII串,并显示出来
;--------------------
(6)如何将AL中的'0'~'9',转换成对应的二进制数,存到指定内存地址(即掌握二进制数与ASCII的互换技巧)
;--------------------
(7)如何将数据段中的 v db '1234','$' 转换成 v2 dw 1234 (因为有了第4步的知识,你可以设计程序看到转换后的结果)
(8)如何使用DOS的0Ah号功能调用从键盘接收一个整数串,并存放到指定缓冲区(就是学习掌握0Ah功能)
(9)如何从键盘接收一个整数串,转换成对应的二进制数后存入到指定内存,如:v1 dw ? 中的v1
(10)如何从键盘接收整数数组(是指利用DOS功能0Ah,接收一个,转换一个,存放一个...)

有了以上这些步的学习,以后你再去做整数的加减乘除什么的。一步步来,你会感觉学习很快乐。
LZ上面的程序就是能运行,也成就不了什么,因为算法设计不是那么科学,对以后的重用大打折扣。看看你写的deci_output,对以后数据的转换利用价值不是那么高,如果整数超过5位,那怎么办,还要做除以1000、10000...?
(以上是我对如何学习汇编的一点思考,难免有不妥,谨供LZ参考)

作者: gsy999   发布时间: 2011-11-17