学 Win32 汇编[8]: 算术运算符、关系操作符、逻辑操作符、高低分离符和移位运算符

学 Win32 汇编[8]: 算术运算符、关系操作符、逻辑操作符、高低分离符和移位运算符


算术运算符 名称 优先级
() 圆括号 1
+,- 正、负 2
*,/ 乘、除 3
MOD 取模 3
+,- 加、减 4

算术运算符示例:
; Test8_1.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib

.code
start:
    PrintDec 7 + 3   ;10
    PrintDec 7 - 3   ;4
    PrintDec 7 * 3   ;21
    PrintDec 7 / 3   ;2
    PrintDec 7 mod 3  ;1
    PrintDec 7 / 3 + 4  ;6
    PrintDec 7 / (3 + 4) ;1
    ret
end start

关系操作符(EQ、NE、LT、LE、GT、GE)示例:

;EQ: 等于
;NE: 不等于
;LT: 小于
;LE: 小于等于
;GT: 大于
;GE: 大于等于

; Test8_2.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib

.code
start:
    ;EQ : =
    PrintDec 2 eq 1  ;0
    PrintDec 2 eq 2  ;-1
    PrintDec 2 eq 3  ;0
    
    ;NE : <>
    PrintDec 2 ne 1  ;-1
    PrintDec 2 ne 2  ;0
    PrintDec 2 ne 3  ;-1
    
    ;LT : <
    PrintDec 2 lt 1  ;0
    PrintDec 2 lt 2  ;0
    PrintDec 2 lt 3  ;-1
    
    ;LE : <=
    PrintDec 2 le 1  ;0
    PrintDec 2 le 2  ;-1
    PrintDec 2 le 3  ;-1
    
    ;GT : >
    PrintDec 2 gt 1  ;-1
    PrintDec 2 gt 2  ;0
    PrintDec 2 gt 3  ;0
    
    ;GE : >=
    PrintDec 2 ge 1  ;-1
    PrintDec 2 ge 2  ;-1
    PrintDec 2 ge 3  ;0
    ret
end start

逻辑操作符(AND、OR、XOR、NOT)示例:
; Test8_3.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib

.code
start:
    PrintHex 0FFFFFFFFh and 0FFFF0000h  ;FFFF0000
    PrintHex 0FFFFFFFFh or  0FFFF0000h  ;FFFFFFFF
    PrintHex 0FFFFFFFFh xor 0FFFF0000h  ;0000FFFF
    PrintHex not 0FFFF0000h             ;0000FFFF
    ret
end start

高低分离符(HIGH、LOW、HIGHWORD、LOWWORD)示例:
; Test8_4.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib

.code
start:
    PrintHex high         1122h  ;11
    PrintHex low          1122h  ;22
    PrintHex high     11223344h  ;33
    PrintHex low      11223344h  ;44
    PrintHex highword 11223344h  ;1122
    PrintHex lowword  11223344h  ;3344
    ret
end start

移位运算符(SHL、SHR)示例:
; Test8_5.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib

.code
start:
    PrintHex 12345678h shl 4  ;23456780
    PrintHex 12345678h shl 8  ;34567800
    PrintHex 12345678h shr 4  ;01234567
    PrintHex 12345678h shr 8  ;00123456
    ret
end start

以上运算符(或伪指令)有些和汇编(硬)指令重名, 区别在于它们只用于常数表达式, 并在编译期出结果.

伪指令? (硬)指令?
MASM 的伪指令是 MASM 提供的, 它们告诉编译期如何编译, 其作用在生成 obj 文件之前;
(硬)指令是 CPU 提供的, 都有对应的二进制码, 它们作用在程序运行时.

posted on 2010-04-04 20:10  万一  阅读(3912)  评论(11)  编辑  收藏
上一篇:学 Win32 汇编[2]: 最简单的 Win32 程序


下一篇:学 Win32 汇编[21] - 传送指令: MOV、LEA、XCHG、XLATB、XLAT、MOVZX、MOVSX