我目前正在尝试学习汇编(Intel x86),并且我制作了一个程序来模拟 32 位字上的位填充 -> 每 5 个连续的相同位(5 个 0 或 5 个 1)插入一个相反的位。为了使字保持其原始的 32 位大小,如果添加填充位,则将截断较低的有效位。
这里有一些例子:
0000 1111 0000 1111 0000 1111 0000 1111 -> 0000 1111 0000 1111 0000 1111 0000 1111
0000 1111 0000 1111 0000 1111 0000 0000 -> 0000 1111 0000 1111 0000 1111 0000 0100
0000 1111 0000 1111 0000 0000 0000 0000 -> 0000 1111 0000 1111 0000 0100 0001 0000
所以这是我的 C++ 程序,它测试一切是否正常,但最后两个不起作用,我不知道为什么。我按照程序使用 IDE 调试器执行的每一步运行了它几次,它似乎完全按照我的意愿执行,但结果不符合...
#include <iostream>
using namespace std;
extern "C" {unsigned int bitstuffing(unsigned int a);}
int main () {
unsigned int in = 0xFFFFFFFF;
unsigned int verif = 0xFBEFBEFB;
unsigned int out = bitstuffing(in);
if (out==verif) cout<<endl<<"OK: "<<hex<<out<<dec<<endl;
else cout<<endl<<"ERROR: "<<hex<<out<<dec<<endl;
in = 0x00000000;
verif = 0x04104104;
out = bitstuffing(in);
if (out==verif) cout<<endl<<"OK: "<<hex<<out<<dec<<endl;
else cout<<endl<<"ERROR: "<<hex<<out<<dec<<endl;
in = 0xF0F0F0F; // 0000 1111 0000 1111 0000 1111 0000 1111
verif = 0xF0F0F0F; // 0000 1111 0000 1111 0000 1111 0000 1111
out = bitstuffing(in);
if (out==verif) cout<<endl<<"OK: "<<hex<<out<<dec<<endl;
else cout<<endl<<"ERROR: "<<hex<<out<<dec<<endl;
in = 0xF0F0F00; // 0000 1111 0000 1111 0000 1111 0000 0000
verif = 0xF0F0F04; // 0000 1111 0000 1111 0000 1111 0000 0100
out = bitstuffing(in);
if (out==verif) cout<<endl<<"OK: "<<hex<<out<<dec<<endl;
else cout<<endl<<"ERROR: "<<hex<<out<<dec<<endl;
in = 0xF0F0000; // 0000 1111 0000 1111 0000 0000 0000 0000
verif = 0xF0F0410; // 0000 1111 0000 1111 0000 0100 0001 0000
out = bitstuffing(in);
if (out==verif) cout<<endl<<"OK: "<<hex<<out<<dec<<endl;
else cout<<endl<<"ERROR: "<<hex<<out<<dec<<endl;
in = 0xAAAA0000; // 1010 1010 1010 1010 0000 0000 0000 0000
verif = 0xAAAA0820; // 1010 1010 1010 1010 0000 1000 0010 0000
out = bitstuffing(in);
if (out==verif) cout<<endl<<"OK: "<<hex<<out<<dec<<endl;
else cout<<endl<<"ERROR: "<<hex<<out<<dec<<endl;
in = 0x7878000; // 0000 0111 1000 0111 1000 0000 0000 0000
verif = 0x7C1F041; // 0000 0111 1100 0001 1111 0000 0100 0001
// out = 0000 0111 1100 0111 1101 0000 0100 0001
out = bitstuffing(in);
if (out==verif) cout<<endl<<"OK: "<<hex<<out<<dec<<endl;
else cout<<endl<<"ERROR: "<<hex<<out<<dec<<endl;
return 0;
}
这就是 ASM 程序,这是最重要的
CPU 386
%include "io.inc"
section .text
global CMAIN
;0FFFFFFFFh - 0xFBEFBEFB ok
;000000000h - 0x04104104 ok
;0F0F0F0Fh - 0xF0F0F0F ok
;0F0F0F00h - 0xF0F0F04 ok
;0F0F0000h - 0xF0F0410 ok
;0AAAA0000h - 0xAAAA0820 DOESNT WORK
;07878000h - 0x7878000 DOESNT WORK
CMAIN:
mov ebp, esp; for correct debugging
PUSH EBP
MOV EBP, ESP
MOV EAX, 07878000h;[EBP+8] ; places message (parameter) in EAX
MOV ECX, 32
MOV BL, 0 ; counts number of "0" bits
MOV BH, 0 ; counts number of "1" bits
loop1:
ROL EAX, 1
JC carry
JNC no_carry
carry:
XOR BL, BL ; resets "0" counter to 0
INC BH ; increments "1" counter
CMP BH, 5 ; if we get 5 consecutive bits of the same sign -> bitstuffing
JE stuffing_0
DEC ECX ; Decrementing ECX for loop
JNZ loop1
JZ end
no_carry:
XOR BH, BH ; resets "1" counter to 0
INC BL ; increments "0" counter
CMP BL, 5 ; if we get 5 consecutive bits of the same sign -> bitstuffing
JE stuffing_1
DEC ECX ; Decrementing ECX for loop
JNZ loop1
JZ end
stuffing_0:
XOR EDX, EDX
XOR EBX, EBX
MOV EDX, 2 ; Putting 2 in EDX for MUL operation
MUL EDX ; Multiplying EAX by 2 is like adding a 0 at the end
XOR EDX, EDX ; Resetting EDX register
DEC ECX ; Decrementing ECX twice for loop (in order to truncate bits)
DEC ECX
CMP ECX, 0
JG loop1
JLE end
stuffing_1:
XOR EDX, EDX
XOR EBX, EBX
MOV EDX, 2 ; Putting 2 in EDX for MUL operation
MUL EDX ; Multiplying EAX by 2 is like adding a 0 at the end
ADD EAX, 1 ; Adding 1 to EAX when the last bit is the zero we added is the same is adding 1 instead of zero
XOR EDX, EDX ; Resetting EDX register
DEC ECX ; Decrementing ECX twice for loop (in order to truncate bits)
DEC ECX
CMP ECX, 0
JG loop1
JLE end
end:
LEAVE
RET
所以当我运行这个程序时,它可以很好地使用以下值(它们都放在 EAX 中)
;0FFFFFFFFh - 0xFBEFBEFB ok
;000000000h - 0x04104104 ok
;0F0F0F0Fh - 0xF0F0F0F ok
;0F0F0F00h - 0xF0F0F04 ok
;0F0F0000h - 0xF0F0410 ok
但不适用于以下
;0AAAA0000h - 0xAAAA0820 DOESNT WORK
;07878000h - 0x7878000 DOESNT WORK
如果有人能发现问题,那将有很大帮助!