这个代码应该怎么优化?

lanhoter 2013-07-08 08:50:00
查了好久, 网上一直都在说 bytes. 不过老师也没讲过,连 .if都没讲过, 实在是不知道如何优化这个代码了, 请大神帮我看看


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

; Build this with the "Project" menu using
; "Console Assemble & Link"

comment * «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

This demo shows how to perform simple addition using registers and assembler
instructions in the first example. The second example shows how to compare
a memory variable to an immediate number and branch to different labels
depending on how large the number is. Branching logic in assembler is very
simple and compact code.

««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««« *

.486
.model flat, stdcall
option casemap :none


include \masm32\include\masm32rt.inc
include \masm32\macros\macros.asm ; MASM support macros

; ------------------------------------------------
; Library files that have definitions for function
; exports and tested reliable prebuilt code.
; ------------------------------------------------
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

; ------------------------------------------------
; Library files that have definitions for function
; exports and tested reliable prebuilt code.
; ------------------------------------------------
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib



.code

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:
call main
exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
main proc

LOCAL str1:DWORD ; space for a DWORD variable
LOCAL var1:DWORD ; a string handle for the input data

; test the MOV and ADD instructions

mov eax, 100
mov ecx, 250
add ecx, eax
print str$(ecx)
print chr$(13,10,13,10)

; ----------------------------------------
; The two following macros can be combined
; once you are familiar with how they work
; ----------------------------------------
;mov str1, input("Enter a number : ")
;mov var1, sval(str1) ; convert the result to a signed integer

mov var1, sval(input("Enter a number : "))

cmp var1, 100
je equal
jg bigger
jl smaller

equal:
print chr$("The number you entered is 100",13,10)
jmp over

bigger:
print chr$("The number you entered is greater than 100",13,10)
jmp over

smaller:
print chr$("The number you entered is smaller than 100",13,10)

over:

ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start
...全文
222 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
WJN92 2013-07-08
  • 打赏
  • 举报
回复
引用 5 楼 lanhoter 的回复:
[quote=引用 4 楼 WJN92 的回复:] 不用print 和 类型转换就优化了。 自己写屏幕,自己写ascii转换。
好像有点深入。。不过老师才教了 加减乘除,比较, 循环, 菜单,打印*, 额。。好纠结的一道题,又占20分,[/quote] 这些其实没有难度网上一大堆。你想老师完全教你,基本没有可能
lanhoter 2013-07-08
  • 打赏
  • 举报
回复
引用 4 楼 WJN92 的回复:
不用print 和 类型转换就优化了。 自己写屏幕,自己写ascii转换。
好像有点深入。。不过老师才教了 加减乘除,比较, 循环, 菜单,打印*, 额。。好纠结的一道题,又占20分,
WJN92 2013-07-08
  • 打赏
  • 举报
回复
不用print 和 类型转换就优化了。 自己写屏幕,自己写ascii转换。
zara 2013-07-08
  • 打赏
  • 举报
回复
建议,实在是不知道啊。要不,就照你说的用 .if 改写下那些个条件判断,这样在程序的结构和易读性上有改进了,也算是种优化?
夜雨无声 2013-07-08
  • 打赏
  • 举报
回复
还有就是可以这样写: cmp var1, 100 je equal jg bigger print chr$("The number you entered is smaller than 100",13,10) ret equal: print chr$("The number you entered is 100",13,10) ret bigger: print chr$("The number you entered is greater than 100",13,10) ret 程序中尽量减少不必要的跳转,可以提高效率。
夜雨无声 2013-07-08
  • 打赏
  • 举报
回复
“equal: print chr$("The number you entered is 100",13,10) jmp over bigger: print chr$("The number you entered is greater than 100",13,10) jmp over” 将以上代码中“jmp over”改为ret。这样可以减少一次跳转,提高一丁点效率:)
lanhoter 2013-07-08
  • 打赏
  • 举报
回复
引用 1 楼 zara 的回复:
优化?你这代码谈不上优化的要求吧。 .if 只是会给程序的结构上更明了些,更易于阅读些,对代码的优化上,性能也好,代码大小也罢,并没有作用,甚至有时还会有负效果。
所以我也很奇怪,这老师发下这个让我们优化一下,然后解释 优化后的 优点...有什么建议改一下吗?
zara 2013-07-08
  • 打赏
  • 举报
回复
优化?你这代码谈不上优化的要求吧。
.if 只是会给程序的结构上更明了些,更易于阅读些,对代码的优化上,性能也好,代码大小也罢,并没有作用,甚至有时还会有负效果。
  • 打赏
  • 举报
回复
除非把大于等于比较做直接跳转处理 ======================================== 除非把大于等于小于比较做直接跳转处理
  • 打赏
  • 举报
回复
这个真的没啥好优化的,除非把大于等于比较做直接跳转处理,结果处理成1、0、-1再映射到三个字符串地址,不过这种优化也很有限,属于回字的若干种写法之一(到现在我也没搞明白回字到底有4种、5种,还是6种写法)。

21,459

社区成员

发帖
与我相关
我的任务
社区描述
汇编语言(Assembly Language)是任何一种用于电子计算机、微处理器、微控制器或其他可编程器件的低级语言,亦称为符号语言。
社区管理员
  • 汇编语言
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧