以下汇编语言代码如何运行?

时间:2015-04-09 16:47:10

标签: assembly x86 masm irvine32

我对集会一无所知,但我得到了这个任务。

请告诉我以下代码是如何运行的?我指的是步骤或程序。

TITLE MASM Template (main.asm)

; Description: 
;
; Revision date:f

INCLUDE Irvine32.inc
.data
counter dword 1;
instruct1 BYTE"How many line are required?: ",0 ;give instruction to user to give the input
answer BYTE"Answer:",0

newline BYTE 0Dh, 0Ah

sum BYTE 0

.code
main PROC
mov edx,OFFSET instruct1 ;move instruct1 to edx
call WriteString
call readint;
mov ecx,eax; move ecx to eax
L1:

push ecx;
mov ecx,counter
L2:
mov al,'*';add '*' into al
call writechar;


loop l2;
pop ecx;
inc counter;
call crlf;
loop l1;



exit
main ENDP

end main

1 个答案:

答案 0 :(得分:1)

此代码打印提示并输入一个数字。然后它打印出那些数量的星星。第一行是1星,第二行是2星,依此类推。我已经注释了代码以使其更清晰。

代码使用两个嵌套循环执行此操作。 ecx寄存器用于两个循环:作为每行上星号的计数器,以及行计数。这就是推送和弹出ecx的原因,因此它可以在内循环中有另一个计数。

TITLE MASM Template (main.asm)      ;used for listings etc.

; Description: 
;
; Revision date:f

INCLUDE Irvine32.inc                ;include another code file

.data                               ;data segment

counter dword 1                     ;characters per line
instruct1 BYTE"How many line are required?: ",0
answer BYTE"Answer:",0              ;irrelevant to this code
newline BYTE 0Dh, 0Ah               ;used by crlf
sum BYTE 0                          ;irrelevant to this code

.code                               ;code segment

    main PROC                       ;declare code block

    mov edx,OFFSET instruct1        ;message pointer
    call WriteString                ;display message
    call readint                    ;input an integer
    mov ecx,eax                     ;move input to line loop register
L1:
    push ecx                        ;save line count register
    mov ecx,counter                 ;load character counter
L2:
    mov al,'*'                      ;the char we wish to print
    call writechar                  ;output one char
    loop L2                         ;next character (count in cx)
    pop ecx                         ;restore the line counter
    inc counter                     ;increment characters per line
    call crlf                       ;print the newline defined above
    loop L1                         ;next line (count in cx)

    exit                            ;return to OS
    main ENDP                       ;end code block
    end main                        ;end of code file

如果输入为3,则输出为:

*
**
***

顺便说一句,我会批评下一行代码的作者,原因有二。

mov ecx,eax     ; move ecx to eax

原因1.评论回到了前面;将返回值eax移至行计数器的ecx

原因2:永远不要使用注释来解释指令的作用,你可以RTM。使用注释来增加价值,明确目的是什么。