【问题标题】:How does the following Assembly language code run?以下汇编语言代码如何运行?
【发布时间】:2015-06-15 03:58:00
【问题描述】:

我对组装一无所知,但我被分配了这个任务。

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

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

【问题讨论】:

  • 猜猜call WriteStringcall readint 做了什么,然后看看字符串和cmets,也许从那里开始工作?
  • 你能解释一下call WriteStringcall readint是什么吗? tq
  • 它的作用是 打印一个由* 字符组成的正方形。所以如果用户输入2,就会打印两行**。如果用户输入 4,它将打印 4 行 ****。如果您要问“它是如何工作的?”,您必须更具体。
  • WriteStringReadInt 是最有可能在包含文件“Irvine32.inc”中提供的函数,该文件包含在文件的顶部。我怀疑WriteString 将字符串写入控制台,ReadInt 读取用户从控制台输入的数字。

标签: assembly x86 masm irvine32


【解决方案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。使用 cmets 来增加价值,明确目的是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    相关资源
    最近更新 更多