首页 文章

8086微处理器的汇编语言[关闭]

提问于
浏览
0

来自stackoverflow的亲切的人 . 所以我是汇编语言的'greenhorn',但是我喜欢编程而且我雄心勃勃,所以最终我也将掌握汇编,但在那之前我需要你的帮助解决问题 . So, i have to write a program for the 8086 microprocessor, that finds all the factors(divisors) of a given number ,我搜索了互联网,但结果对我来说很难理解 . 所以我和我的大脑在C中编写了程序,但现在我的大脑不知道如何将它转换为汇编语言 .

#include <stdio.h>
int main()
{
    int number, i;

    printf("Enter a number: ");
    scanf("%d", &number);

    printf("The divisors(factors) for %d are: ", number);
    for(i = 0; i <= number; i++)
    {
        if(number % i == 0)
           printf("%d", i);
    }
   return 0;
}

那么,我如何在assambly中做到这一点?这是现在做了多少,但可能只是糟糕的代码:

Date:

number dw 54 ;dw because I work with 16 bits
i      dw 2
result dw ? ; the actual result

CODE:

mov ax, number ; I move the value of number in ax(16 bits)
mov bx, i

loop:
div bx ; number % i
cmp bx, 0; compare the modulo with 0
je final ; jump to final if is true
mov dx, 0
mov ax, bx
mov bx, dx
jmp loop ; jump from loop

;So...what now?...      

final:

move result, ax;move what i have in ax to result

move ax, 4C00h ; i really don't know what is this...i just know i has to be at the end
int 21h ; an integer

拜托,你能帮帮我吗?注意:我尝试安装TASM来测试我的代码,但它不起作用,我觉得它只适用于Windows 7,但我不确定 . 所以,我需要你告诉我代码是好还是坏 .

1 回答

  • 1

    首先,我建议你从找到最小的除数开始 . 因为正如我在上面看到的那样,当模数为零时,你将进入最终标签 . 因此,当它找到一个除数时,它会跳到那里并完成 .

    我的第二个建议是,当你正在研究MUL和DIV指令时,请仔细使用它们 . 当你做DIV BX模数不会在BX上 . 它将在DX上 . 因为你的除数是16位(这是BX) . 因此,8086将执行DX:AX / BX,其中包括:AX和剩余:DX(模数) .

    就像我说的,如果你是新手,你应该先在DIV和MUL上练习更简单的应用程序 .

相关问题