如何使用汇编语言打印0,2,4,6,...

时间:2021-01-30 07:09:10

I have an assignment from my comp. system org. subject and unfortunately I'm kind of new when it comes to assembly language. I'm supposed to write a program that displays the numbers 0,2,4,6,8,10 respectively. How would I go about this?

我有一份来自我的作业的作业。系统组织。主题,不幸的是,当谈到汇编语言时,我有点新鲜。我应该编写一个程序,分别显示数字0,2,4,6,8,10。我怎么会这样呢?

Maybe this'll answer my question: (Reactions please)

也许这会回答我的问题:(反应请)

.model small
.stack 100H
.data
.code

call proc

mov cx,5

mov dx,0
L1:
mov bx,2
add dx,bx
mov ah,02h
loop L1
int 21
endp

8 个解决方案

#1


Go see your lecturer and/or tutor and ask for advice. That's what they're there for. You haven't given us anywhere near enough info to help you out.

去看你的讲师和/或导师,并征求意见。这就是他们的目的。您没有给我们任何足够的信息来帮助您。

Here's what I think your ABCD program should look like. I suggest you use it as a baseline to try to make a 0 2 4 ... version.

这就是我认为您的ABCD计划应该是什么样子。我建议你用它作为基线试图制作一个0 2 4 ...版本。

    model  proc
    .stack 100H
    .data
    .call

    main   proc

    mov    cx,10     ; 10 loops only.
    mov    dx,40h    ; start dx at 'A' - 1.
L1:
    inc    dx        ; move to next character.

    mov    ah,02h    ; int 21,02 is print character.
    int    21h

    loop   L1        ; loop until cx is 0

    mov    ax,4c00h  ; int 21,4c is exit with al holding exit code.
    int    21

    endp

When you've at least had a go at converting this, post the code and we'll critique what you've done.

如果您至少要转换它,请发布代码,我们会批评您所做的事情。

If you're taught something, it never lasts but, if you learn something, it lasts forever (alcohol-addled braincells notwithstanding :-).

如果你被教导的东西,它永远不会持续,但是,如果你学到了什么,它会永远存在(尽管有酒精添加的braincells :-)。

Int 21 is the DOS interrupt which allows assembler programs to use various DOS functions. It's conceptually a huge switch statement based on the AH register which is why you'll see things like Int 21 Fn 02, which means execute mov ah,2 followed by int 21.

Int 21是DOS中断,允许汇编程序使用各种DOS函数。它在概念上是一个基于AH寄存器的巨大的switch语句,这就是为什么你会看到像Int 21 Fn 02这样的东西,这意味着执行mov ah,2然后是int 21。

Int 21 Fn 02 will take the contents of DL and output that to the screen. So the sequence:

Int 21 Fn 02将获取DL的内容并将其输出到屏幕。顺序如下:

mov ah,02h
mov dl,41h
int 21h

will output the 'A' character (0x41).

将输出'A'字符(0x41)。

Similarly, Int 21 Fn 4c will exit the current running process.

同样,Int 21 Fn 4c将退出当前运行的进程。

#2


Assembly language is a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU (or architecture). So assembly language for Macs (most recently Intel's X86) is different from that used to on the iPhone - ARM.

汇编语言是编程特定CPU(或体系结构)所需的数字机器代码和其他常量的符号表示。因此,Mac的汇编语言(最近的英特尔X86)与iPhone上的汇编语言不同 - ARM。

Your teacher is also probably expecting you to realise the difference between the binary form of the number you will count with, and the ASCII format you will use to display to the screen.

您的老师也可能期望您意识到您将使用的数字的二进制形式与您将用于显示到屏幕的ASCII格式之间的差异。

#3


I'm sure your class gave you some education here. Can you code enough assembly to print one or two numbers? Can you code enough to calculate the numbers, even if you can't print them?

我相信你的班级在这里给你一些教育。你可以编写足够的程序集来打印一个或两个数字吗?即使你不能打印它们,你能编写足够的代码来计算数字吗?

Post that much code, and you may find help here.

发布那么多代码,你可以在这里找到帮助。

Otherwise, you're asking others to actually do your homework for you.

否则,你要求其他人真正为你做功课。

#4


You can do it exactly like the program which prints A, B, C, D, etc.: except that instead of starting at 'A', start at '0; and instead of increasing by 1 each time (from 'A' to 'B'), increase by 2 (from '0' to '2').

你可以完全像打印A,B,C,D等的程序那样做,除了不是从'A'开始,从'0开始;而不是每次增加1(从'A'到'B'),增加2(从'0'到'2')。

After printing '0', '2', '4', '6', and '8', the next number that you want to print is '10'.

打印“0”,“2”,“4”,“6”和“8”后,您要打印的下一个数字是“10”。

To print '10', you can print '1' followed by '0'. Or, instead of invoking int 21 with ah=2 (which prints one character at a time), you can set ah=9 to print a string (set ds:dx to a block of memory which contains "10$").

要打印'10',您可以打印'1',然后打印'0'。或者,不是使用ah = 2(一次打印一个字符)调用int 21,而是可以设置ah = 9来打印字符串(将ds:dx设置为包含“10 $”的内存块)。


Later you suggested the following solution and asked for criticism:

后来你提出了以下解决方案并要求批评:

.model small
.stack 100H
.data
.code

main proc

call defineuser1
call defineuser2
mov cx,5

userdefine1 proc
L1:
mov dx,0
mov bx,2
add dx,bx
mov ah,02h
loop L1
int 21h
endp

userdefine2 proc
mov ah, 4ch
int 21h
userdefine2
endp

My criticisms are as follows:

我的批评如下:

  • defineuser1 doesn't exist (I think you mean userdefine1)

    defineuser1不存在(我认为你的意思是userdefine1)

  • setting cx needs to be inside (not before) the procedure

    设置cx需要在程序内部(而不是之前)

  • invoking int 21 needs to be inside (not outside) the loop

    调用int 21需要在循环内部(而不是外部)

  • you need special handling for "10" as I mentioned above

    如上所述,你需要对“10”进行特殊处理

  • There's a difference between '0' (the ASCII character/digit) and 0 (the number) ... you need to print the character/digit, not the number

    '0'(ASCII字符/数字)和0(数字)之间有区别...你需要打印字符/数字,而不是数字

  • You need to learn to test your code (write it, step through it with debugger, and debug it), preferably before you post questions about it.

    您需要学习测试代码(编写代码,使用调试器逐步调试并调试代码),最好在发布有关代码的问题之前。

#5


You do know there is more than one flavor of "Assembly Language."

你知道有不止一种“汇编语言”的风格。

#6


You would have a counter beginning at zero and repeatedly increment it by two, printing the result.

您将有一个从零开始的计数器并重复递增2,打印结果。

#7


MOV AX, 0
MOV BX, 2

ADDLOOP:
ADD AX, BX
CMP AX, 10
JE DONE
JMP ADDLOOP

DONE:

Ok. That's my best attempt. Lots of details left out. I should also mention that I have no frigging clue how to print a char to the screen.

好。那是我最好的尝试。遗漏了很多细节。我还应该提一下,我没有任何关于如何在屏幕上打印字符的线索。

Like others have mentioned, you didn't specify which assembly language so I chose x86.

像其他人提到的那样,你没有指定哪种汇编语言,所以我选择了x86。

Finally, go talk to your instructors, they'll help you much more than we can.

最后,与您的教师交谈,他们会比我们更多地帮助您。

#8


Are you using a macro for the output?

你是否使用宏输出?

should be something like...

应该像......

 

        mov eax, 0

myloop: cmp eax, 10 jg done output macro eax add eax, 2 jmp myloop done:

myloop:cmp eax,10 jg完成输出宏eax add eax,2 jmp myloop完成:

of course that's for 8086 assembly.

当然这是8086组装。

#1


Go see your lecturer and/or tutor and ask for advice. That's what they're there for. You haven't given us anywhere near enough info to help you out.

去看你的讲师和/或导师,并征求意见。这就是他们的目的。您没有给我们任何足够的信息来帮助您。

Here's what I think your ABCD program should look like. I suggest you use it as a baseline to try to make a 0 2 4 ... version.

这就是我认为您的ABCD计划应该是什么样子。我建议你用它作为基线试图制作一个0 2 4 ...版本。

    model  proc
    .stack 100H
    .data
    .call

    main   proc

    mov    cx,10     ; 10 loops only.
    mov    dx,40h    ; start dx at 'A' - 1.
L1:
    inc    dx        ; move to next character.

    mov    ah,02h    ; int 21,02 is print character.
    int    21h

    loop   L1        ; loop until cx is 0

    mov    ax,4c00h  ; int 21,4c is exit with al holding exit code.
    int    21

    endp

When you've at least had a go at converting this, post the code and we'll critique what you've done.

如果您至少要转换它,请发布代码,我们会批评您所做的事情。

If you're taught something, it never lasts but, if you learn something, it lasts forever (alcohol-addled braincells notwithstanding :-).

如果你被教导的东西,它永远不会持续,但是,如果你学到了什么,它会永远存在(尽管有酒精添加的braincells :-)。

Int 21 is the DOS interrupt which allows assembler programs to use various DOS functions. It's conceptually a huge switch statement based on the AH register which is why you'll see things like Int 21 Fn 02, which means execute mov ah,2 followed by int 21.

Int 21是DOS中断,允许汇编程序使用各种DOS函数。它在概念上是一个基于AH寄存器的巨大的switch语句,这就是为什么你会看到像Int 21 Fn 02这样的东西,这意味着执行mov ah,2然后是int 21。

Int 21 Fn 02 will take the contents of DL and output that to the screen. So the sequence:

Int 21 Fn 02将获取DL的内容并将其输出到屏幕。顺序如下:

mov ah,02h
mov dl,41h
int 21h

will output the 'A' character (0x41).

将输出'A'字符(0x41)。

Similarly, Int 21 Fn 4c will exit the current running process.

同样,Int 21 Fn 4c将退出当前运行的进程。

#2


Assembly language is a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU (or architecture). So assembly language for Macs (most recently Intel's X86) is different from that used to on the iPhone - ARM.

汇编语言是编程特定CPU(或体系结构)所需的数字机器代码和其他常量的符号表示。因此,Mac的汇编语言(最近的英特尔X86)与iPhone上的汇编语言不同 - ARM。

Your teacher is also probably expecting you to realise the difference between the binary form of the number you will count with, and the ASCII format you will use to display to the screen.

您的老师也可能期望您意识到您将使用的数字的二进制形式与您将用于显示到屏幕的ASCII格式之间的差异。

#3


I'm sure your class gave you some education here. Can you code enough assembly to print one or two numbers? Can you code enough to calculate the numbers, even if you can't print them?

我相信你的班级在这里给你一些教育。你可以编写足够的程序集来打印一个或两个数字吗?即使你不能打印它们,你能编写足够的代码来计算数字吗?

Post that much code, and you may find help here.

发布那么多代码,你可以在这里找到帮助。

Otherwise, you're asking others to actually do your homework for you.

否则,你要求其他人真正为你做功课。

#4


You can do it exactly like the program which prints A, B, C, D, etc.: except that instead of starting at 'A', start at '0; and instead of increasing by 1 each time (from 'A' to 'B'), increase by 2 (from '0' to '2').

你可以完全像打印A,B,C,D等的程序那样做,除了不是从'A'开始,从'0开始;而不是每次增加1(从'A'到'B'),增加2(从'0'到'2')。

After printing '0', '2', '4', '6', and '8', the next number that you want to print is '10'.

打印“0”,“2”,“4”,“6”和“8”后,您要打印的下一个数字是“10”。

To print '10', you can print '1' followed by '0'. Or, instead of invoking int 21 with ah=2 (which prints one character at a time), you can set ah=9 to print a string (set ds:dx to a block of memory which contains "10$").

要打印'10',您可以打印'1',然后打印'0'。或者,不是使用ah = 2(一次打印一个字符)调用int 21,而是可以设置ah = 9来打印字符串(将ds:dx设置为包含“10 $”的内存块)。


Later you suggested the following solution and asked for criticism:

后来你提出了以下解决方案并要求批评:

.model small
.stack 100H
.data
.code

main proc

call defineuser1
call defineuser2
mov cx,5

userdefine1 proc
L1:
mov dx,0
mov bx,2
add dx,bx
mov ah,02h
loop L1
int 21h
endp

userdefine2 proc
mov ah, 4ch
int 21h
userdefine2
endp

My criticisms are as follows:

我的批评如下:

  • defineuser1 doesn't exist (I think you mean userdefine1)

    defineuser1不存在(我认为你的意思是userdefine1)

  • setting cx needs to be inside (not before) the procedure

    设置cx需要在程序内部(而不是之前)

  • invoking int 21 needs to be inside (not outside) the loop

    调用int 21需要在循环内部(而不是外部)

  • you need special handling for "10" as I mentioned above

    如上所述,你需要对“10”进行特殊处理

  • There's a difference between '0' (the ASCII character/digit) and 0 (the number) ... you need to print the character/digit, not the number

    '0'(ASCII字符/数字)和0(数字)之间有区别...你需要打印字符/数字,而不是数字

  • You need to learn to test your code (write it, step through it with debugger, and debug it), preferably before you post questions about it.

    您需要学习测试代码(编写代码,使用调试器逐步调试并调试代码),最好在发布有关代码的问题之前。

#5


You do know there is more than one flavor of "Assembly Language."

你知道有不止一种“汇编语言”的风格。

#6


You would have a counter beginning at zero and repeatedly increment it by two, printing the result.

您将有一个从零开始的计数器并重复递增2,打印结果。

#7


MOV AX, 0
MOV BX, 2

ADDLOOP:
ADD AX, BX
CMP AX, 10
JE DONE
JMP ADDLOOP

DONE:

Ok. That's my best attempt. Lots of details left out. I should also mention that I have no frigging clue how to print a char to the screen.

好。那是我最好的尝试。遗漏了很多细节。我还应该提一下,我没有任何关于如何在屏幕上打印字符的线索。

Like others have mentioned, you didn't specify which assembly language so I chose x86.

像其他人提到的那样,你没有指定哪种汇编语言,所以我选择了x86。

Finally, go talk to your instructors, they'll help you much more than we can.

最后,与您的教师交谈,他们会比我们更多地帮助您。

#8


Are you using a macro for the output?

你是否使用宏输出?

should be something like...

应该像......

 

        mov eax, 0

myloop: cmp eax, 10 jg done output macro eax add eax, 2 jmp myloop done:

myloop:cmp eax,10 jg完成输出宏eax add eax,2 jmp myloop完成:

of course that's for 8086 assembly.

当然这是8086组装。