-2

问题是“就像可以通过反复加法进行乘法一样,也可以通过反复减法进行除法。编写一个程序,计算一个数字 N1 与另一个数字 N2 的次数。你将需要一个循环,并计算该循环执行了多少次”。我真的被困在减法阶段。我知道我必须创建一个循环,但我不知道在哪里放置它。

org 100h

.MODEL SMALL
.STACK 100H
.DATA

MSG1 DB 'FIRST > $'
MSG2 DB 'SECOND > $'
MSG3 DB 'THE SUBTRACTION OF '
VALUE1 DB ?
MSG4 DB ' AND '


VALUE2 DB ?, ' IS '

SUM DB ?,'.$'

CR DB 0DH, 0AH, '$'

.CODE
MAIN PROC
;INITIALIZE DS
MOV AX, @DATA
MOV DS, AX 
;PROMPT FOR FIRST INPUT
LEA DX, MSG1
MOV AH, 9H
INT 21H
MOV AH, 1H
INT 21H
MOV VALUE1, AL
MOV BH, AL
SUB BH, '0'
;CARRIAGE RETURN FORM FEED
LEA DX, CR
MOV AH, 9H
INT 21H
;PROMPT FOR SECOND INPUT
LEA DX, MSG2
MOV AH, 9H
INT 21H
MOV AH, 1H
INT 21H
MOV VALUE2, AL
MOV BL, AL
SUB BL, '0'
SUBTRACT:
;SUB THE VALUES CONVERT TO CHARACTER AND SAVE
SUB BH, BL
ADD BH, '0'
MOV SUM, BH
;CARRIAGE RETURN FORM FEED
LEA DX, CR
MOV AH, 9H
INT 21H
;OUTPUT THE RESULT
LEA DX, MSG3
MOV AH, 9H
INT 21H


TERMINATE:
;RETURN TO DOS
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
4

2 回答 2

1

正 N1,N2 的算法:

  1. 准备N1N2并将一些设置R为-1
  2. 增量R
  3. 减去N1(用结果N2更新)N2
  4. 当减法结果大于或等于零时,转到步骤 2。
  5. R有整数除法的结果N2/N1

步骤 2. 到 4. 可以在 x86 汇编中通过每一步的单个指令编写(sub更新“进位标志”,可用于确定减法是否“溢出”-由“Jcc”之一使用 = jump-条件代码指令要么跳转到其他地方,要么继续下一条指令)。

于 2016-10-13T11:01:10.457 回答
0

下一个程序完成这项工作。这些数字在数据段中被声明为变量,注释解释了一切(只需将其复制粘贴到 EMU8086 中并运行它):

.model small
.stack 100h
.data

n1    dw 3
n2    dw 95
count dw ?
msg   db 'Quotient = $'
str   db 10 dup('$')

.code
  mov ax, @data
  mov ds, ax

;DIVIDE BY SUBTRACTIONS.             
  mov count, 0    ;COUNTER FOR SUBTRACTIONS.
subtract:
;CHECK IF SUBTRACTION CAN BE DONE.
  mov ax, n1      ;COPY N1 INTO AX BECAUSE CAN...
  cmp n2, ax      ;...NOT COMPARE MEMORY-MEMORY.
  jl  finale      ;N2 < N1. NO MORE SUBTRACTIONS.
;SUBTRACT.
  sub n2, ax      ;N2-N1.
  inc count       ;INCREASE SUBTRACTIONS COUNTER.
  jmp subtract    ;REPEAT.  

finale:
;DISPLAY MESSAGE.
  mov ah, 9
  mov dx, offset msg
  int 21h
;CONVERT QUOTIENT (COUNT) INTO STRING.
  mov ax, count
  mov si, offset str
  call number2string
;DISPLAY COUNT (QUOTIENT).
  mov ah, 9
  mov dx, offset str
  int 21h

;WAIT FOR A KEY TO BE PRESSED.         
  mov ah, 0
  int 16h

;EXIT.  
  mov ax, 4c00h
  int 21h

;------------------------------------------
;CONVERT A NUMBER IN STRING.
;ALGORITHM : EXTRACT DIGITS ONE BY ONE, STORE
;THEM IN STACK, THEN EXTRACT THEM IN REVERSE
;ORDER TO CONSTRUCT STRING (STR).
;PARAMETERS : AX = NUMBER TO CONVERT.
;             SI = POINTING WHERE TO STORE STRING.

number2string proc 
  mov  bx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
  mov  cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
cycle1:       
  mov  dx, 0 ;NECESSARY TO DIVIDE BY BX.
  div  bx ;DX:AX / 10 = AX:QUOTIENT DX:REMAINDER.
  push dx ;PRESERVE DIGIT EXTRACTED FOR LATER.
  inc  cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.
  cmp  ax, 0  ;IF NUMBER IS
  jne  cycle1 ;NOT ZERO, LOOP. 
;NOW RETRIEVE PUSHED DIGITS.
cycle2:  
  pop  dx        
  add  dl, 48 ;CONVERT DIGIT TO CHARACTER.
  mov  [ si ], dl
  inc  si
  loop cycle2  

  ret
number2string endp  

过程“number2string”用于将结果转换为字符串,如果该结果有多个数字,则必须这样做。

请注意,除数 (N2) 和除数 (N1) 都不是从键盘捕获的,它们是数据段中的静态值。为了从键盘捕获它们,我们需要另一个过程“string2number”,将字符串转换为数字格式。

于 2016-10-13T14:26:46.610 回答