【问题标题】:Division by subtraction [closed]减法除法[关闭]
【发布时间】:2013-12-05 17:37:59
【问题描述】:

问题:

将 A 分为 B ,假设 A

示例:7 进入 20 2 乘以余数 6

提示:从 B 中减去 A 直到 A> 差。数一下多少次 你减去,差应该是余数。

Ex  20-7 = 13 
    13 – 7 = 6
    6 <7 so the count is 2 and the remainder is 6

这是我的代码。它不完整。我不知道如何解决这个问题。任何帮助将不胜感激。

TITLE       PROJECT
INCLUDE Irvine32.inc
.data
prompt1 byte 'Enter number A:',0
prompt2 byte 'Enter number B:',0
a dword ?
b dword ?
remainder dword ?
.code
main proc
    call clrscr

    mov eax,0
    mov ebx,0
    mov edx,offset prompt1
    call writestring
    call readint
    mov a,eax

    mov edx,offset prompt2
    call writestring
    call readint
    mov b,ebx

    mov eax,a
    mov ebx,b
    sub ebx,a       ;set edx to 0
    div ebx
    mov remainder,ebx
    ;xor eax,eax


    call writedec
    call crlf

exit
main ENDP
END main

【问题讨论】:

  • 那么你的循环在哪里?你有什么问题?
  • 抱歉回复晚了。我不知道如何计算余数。

标签: assembly x86 masm irvine32


【解决方案1】:

一个循环是这样实现的:

循环往下:

    mov   ecx, 10   ; count from 10 to 0

 label:
    dec   ecx
    loop label

循环:

   xor  ecx,ecx   ; count from 0 to 10
 label:
   inc ecx
   cmp ecx, 10
   jne label

现在想想你的算法是如何工作的,以及如何应用这些信息。

【讨论】:

  • mov eax,a mov ebx,b sub ebx,a xor ecx,ecx ;从 0 计数到 10 标签:inc ecx cmp ecx, 10 jne label 我可以将您的代码放在我的代码下以使其通过循环。
  • 对不起,我不知道如何在评论中写代码..
  • 你有其他语言的知识吗,比如C?如果是,那么您可能想先用 C 语言编写算法并保持简单。尽量限制自己只使用少量变量,因为 CPU 没有那么多寄存器。这应该可以帮助您了解程序集的外观。
  • 感谢提醒..
猜你喜欢
  • 2014-12-18
  • 2013-10-09
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多