Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 新装配;我是否遵守此程序的适当代码标准?如果没有,欢迎提供提示_Performance_Assembly_Code Analysis_Masm - Fatal编程技术网

Performance 新装配;我是否遵守此程序的适当代码标准?如果没有,欢迎提供提示

Performance 新装配;我是否遵守此程序的适当代码标准?如果没有,欢迎提供提示,performance,assembly,code-analysis,masm,Performance,Assembly,Code Analysis,Masm,你好 我一个月前开始汇编编程,在stackoverflow的几本书和社区的帮助下,我一直在用ASM编写程序。我写这个程序是为了比较两个数字,然后打印哪个数字更大。我想知道我是否做得对。欢迎提供任何性能优化提示 .386 .model flat, stdcall option casemap:none include \masm32\include\windows.inc include \masm32\include\kernel32.inc include \masm32\include\

你好 我一个月前开始汇编编程,在stackoverflow的几本书和社区的帮助下,我一直在用ASM编写程序。我写这个程序是为了比较两个数字,然后打印哪个数字更大。我想知道我是否做得对。欢迎提供任何性能优化提示

.386
.model flat, stdcall

option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
include \masm32\include\user32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\kernel32.lib

.data
prompt BYTE "Enter First Number:",13,10,0
prompt1 BYTE "Enter Second Number:",13,10,0
prompt2 BYTE "First number greater than second number",13,10,0
prompt3 BYTE "Second number greater than first number",13,10,0
prompt4 BYTE "Both numbers are equal",13,10,0
.data?
outputHandle DWORD ?
inputHandle DWORD ?
nCharsWritten DWORD ?
len1 DWORD ?
buf1 BYTE 200 DUP(?)
len2 DWORD ?
buf2 BYTE 200 DUP(?)
num1 DWORD ?
num2 DWORD ?

.code


main PROC
                ;get output and input handles
                ;first get output handle
                push STD_OUTPUT_HANDLE
                CALL GetStdHandle
                ;save output handle
                mov outputHandle, eax
                ;now get input handle
                push STD_INPUT_HANDLE
                CALL GetStdHandle
                ;save input handle
                mov inputHandle, eax

                ;Ask User for First Number
                push  NULL
                push OFFSET nCharsWritten
                mov eax, LENGTHOF prompt
                dec eax
                push eax
                push OFFSET prompt 
                push outputHandle
                CALL WriteConsoleA

                ;Input First Number
                push NULL
                push OFFSET len1
                push 200
                push OFFSET buf1
                push inputHandle
                CALL ReadConsoleA

                ;prompt user for second number
                push NULL
                push OFFSET nCharsWritten
                mov eax, LENGTHOF prompt1
                dec eax
                push eax
                push OFFSET prompt1
                push outputHandle
                CALL WriteConsoleA

                ;Input Second Number
                push NULL
                push OFFSET len2
                push 200
                push OFFSET buf2
                push inputHandle
                CALL ReadConsoleA

                ;Strip CRLF
                push OFFSET buf1
                CALL StripLF
                push OFFSET buf2
                CALL StripLF

                ;Convert OEM to char
                push OFFSET buf1
                push OFFSET buf1
                CALL OemToChar
                push OFFSET buf2
                push OFFSET buf2
                CALL OemToChar

                ;Convert string to decimal
                push OFFSET buf1
                CALL atodw
                mov num1, eax
                push OFFSET buf2
                CALL atodw
                mov num2, eax
                ;Clear ZF 
                or al,1
                ;Clear CF
                clc
                ;Compare the two numbers
                mov eax, num2                   
                cmp eax, num1               
                jl L1                   ;jump if num2 is less than num1                         
                jg L2                   ;jump if num2 is greater than num1      
                ;both equal
                ;write to console
                push NULL
                push OFFSET nCharsWritten
                push LENGTHOF prompt4
                push OFFSET prompt4
                push outputHandle
                CALL WriteConsoleA
                jmp L3
            L1:
                ;Write to console
                push NULL
                push OFFSET nCharsWritten
                push LENGTHOF prompt2
                push OFFSET prompt2
                push outputHandle
                CALL WriteConsoleA
                jmp L3

            L2:
                ;Write to console
                push NULL
                push OFFSET nCharsWritten
                push LENGTHOF prompt3
                push OFFSET prompt3
                push outputHandle
                CALL WriteConsoleA
                jmp L3

            L3:
                push NULL
                CALL ExitProcess
        main ENDP
        END main

一个小的优化可能是将对WriteConsoleA的公共调用移动到L3之后的所有比较结束:

.
.
    ;Compare the two numbers
    mov eax, num2                   
    cmp eax, num1               
    jl L1                   ;jump if num2 is less than num1                         
    jg L2                   ;jump if num2 is greater than num1      
    ;both equal
    ;write to console
    push NULL
    push OFFSET nCharsWritten
    push LENGTHOF prompt4
    push OFFSET prompt4
    push outputHandle
    ; CALL WriteConsoleA <- remove
    jmp L3
L1:
    ;Write to console
    push NULL
    push OFFSET nCharsWritten
    push LENGTHOF prompt2
    push OFFSET prompt2
    push outputHandle
    ; CALL WriteConsoleA <- remove
    jmp L3

L2:
    ;Write to console
    push NULL
    push OFFSET nCharsWritten
    push LENGTHOF prompt3
    push OFFSET prompt3
    push outputHandle
    ; CALL WriteConsoleA ; <- remove
    ; jmp L3 ; unnecessary unless new code added in-between

L3:
    CALL WriteConsoleA
    push NULL
    CALL ExitProcess
.
.
。
.
;比较这两个数字
mov-eax,num2
cmp-eax,num1
jl-L1;如果num2小于num1,则跳转
jgl2;如果num2大于num1,则跳转
;两者相等
;写入控制台
推空
写入的推送偏移量
推送长度提示4
推送偏置prompt4
推手

; 调用WriteConsoleA有意义的标签名称如何?

许多事情需要做得更好。首先,您使用的是Masm,但不是它的功能。首先要注意的是,您不使用invoke或procs,而是使用push/call模型和直接的代码流

我将高度推荐iczelion的优秀教程:

了解Masm的一些功能的第一个不错的教程是:


谢谢你,伙计!这在许多其他程序中也很方便!这是一个小程序,所以我想我只需要使用短标签名称,它们可以提供信息,甚至更短,比如g:,l:,out:。我更喜欢使用推送/调用模型,这就是我使用它的原因:)我可以向你保证,如果你开始编写大型程序,你会很快放弃它:)