[英]how to convert a c program with many variables to assembly
我必須將這個C程序轉換為匯編,但主寄存器不夠用,如何在匯編中添加更多變量? (不從 C 導入它們)或者是否有一種簡單的方法可以在匯編中重現此代碼?
該程序的目標是比較最小的字符串( S2
)和最大的( S1
),並在字符串1中每次重復時將單詞的第一個字符的position保存在數組positions[]
中
因此,如果
s1 = hello hello hellk
s2 = he
預期的結果是
positions [0] = 0
positions [1] = 6
positions [2] = 12
positionsLen = 3
int main()
{
#define MAX_LEN 100
//input
char s1[] = "hello hello hellk"; //17 char
unsigned int lenS1 = sizeof(s1) - 1;
char s2[] = "he"; //2 char
unsigned int lenS2 = sizeof(s2) - 1;
//output
unsigned int positions[MAX_LEN];
unsigned int positionsLen;
////-------------------------------------------------------
int i, j, found;
int stringLen, searchLen;
stringLen = lungS1; // length of string
searchLen = lungS2; // length of word to be searched
positionsLen = 0; //positions[] index
for (i = 0; i <= stringLen - searchLen; i++) //repeats the times the string s2 enters the string s1
{
//flag the word like found
found = 1;
for (j = 0; j < searchLen; j++) //scroll through the characters of the string 2
{
if (s1[i + j] != s2[j]) //I compare the two strings character by character (the number of times as the size of the string 2)
//if the characters are different, the word is not found
{
found = 0;
break;
}
}
if (found == 1)
//if the word was found from position i to position i + searchlen
{
positions[positionsLen] = i;
positionsLen++;
}
}
return 0;
復制這個匯編程序的解決方案是什么? 這是我的嘗試,但由於寄存器太小而導致溢出問題。
__asm
{
//dichiaro le variabili
XOR AH, AH //i + j
XOR AL, AL //s1
XOR BH, BH //found
XOR BL, BL //scambio
XOR ECX, ECX //i e j ma uso solo CX
XOR DH, DH //volte
XOR DL, DL //s2
MOV DH, lungS1
SUB DH, lungS2 //volte sarà la differenza tra le due stringhe e quindi le volte che il loop esterno si ripeterà
MOV positionsLen, 0 //assegno la lunghezza del vettore
MOV CX, 0 //imposto la i a 0
ext_loop: nop //for con i
MOV BH, 1 //FLAG TROVATO A 1
MOV BL, CX //BL = CX (i)
MOV CX, 0 //imposto la j a 0
/////////////////////////////////////////////////////
int_loop : //for con j
//devo rendere AH == i+J
//quindi
MOV AH, BL //AH = i
ADD AH, CX //AH = AH + CX(j)
MOV AL, s1[AH]
MOV DL, s2[CX]
CMP AL, DL // CMP S1[i+j] == s2[j]
je jump1//se sono uguali salta alla fine dell'if
MOV BH, 0 //FLAG TROVATO A 0
jmp int_loop_end //BREAK
jump1 :
INC CX //incremento j
CMP CX, lungS2 //se j<lungS2 il ciclo si ripete
jl int_loop
/////////////////////////////////////////////////////
int_loop_end :
//tramite BL che non ho modificato nel ciclo interno
//faccio tornare cx la variabile i.
//(la j non mi servirà più fino al prossimo ciclo interno)
MOV CX, BL
MOV BL, 0
//inizio l'if per verificare che sia stata trovata
//la parola che inizia alla posizione i
test BH, BH //se uguale a 0 salta
jz jump2
MOV positions[positionsLen], CX
INC positionsLen
jump2 :
INC CX //INCREMENTO LA i
CMP CX, DH //COMPARO LA i A volte
jl ext_loop //SE MINORE CONTINUA
}
自己寫匯編代碼復制一個C
的代碼是非常困難和耗時的過程。 但是,您可以使用任何編譯器( gcc
、 clang
或msvc
)輕松執行此操作,它將生成C/C++
代碼的匯編代碼。
假設您的C
文件名為main.c :
使用此命令生成名為main.s
的匯編代碼:
gcc main.c -S
您可以使用優化標志優化最終的匯編代碼:
gcc -O3 -s -Og -masm=intel -march=native main.c -S
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.