簡體   English   中英

匯編語言-工作原理

[英]Assembly language - How it works

我真的是學習匯編語言的新手,並且剛剛開始研究匯編語言,所以我想知道也許你們中的某些人可以幫助我解決一個問題。 我有一個作業,告訴我將匯編語言指令與C代碼進行比較,並告訴我哪些C代碼與匯編指令等效。 所以這是組裝說明:

pushl %ebp // What i think is happening here is that we are creating more space for the function.
movl %esp,%ebp // Here i think we are moving the stack pointer to the old base pointer.
movl 8(%ebp),%edx // Here we are taking parameter int a and storing it in %edx
movl 12(%ebp),%eax // Here we are taking parameter int b and storing it in %eax
cmpl %eax,%edx // Here i think we are comparing int a and b ( b > a ) ?
jge .L3 // Jump to .L3 if b is greater than a - else continue the instructions
movl %edx,%eax // If the term is not met here it will return b
.L3:
movl %ebp,%esp // Starting to finish the function
popl %ebp // Putting the base pointer in the right place
ret // return

我試圖基於對這一點的理解將其注釋掉-但是我對此可能完全錯了。 C函數的選項之一被認為是等效的:

int fun1(int a, int b)
{
unsigned ua = (unsigned) a;
if (ua < b)
return b;
else
return ua;
}
int fun2(int a, int b)
{
if (b < a)
return b;
else
return a;
}
int fun3(int a, int b)
{
if (a < b)
return a;
else
return b;
}

我認為正確答案是fun3 ..但我不太確定。

首先,歡迎使用StackOverflow。 很棒的地方,真的是。

現在開始吧,讓我來幫助您; 很多; 很多。

您有很好的評論,可以極大地幫助您和我以及其他所有人,但是它們太丑陋,以致閱讀起來很痛苦。

解決方法如下:空格,大量空格,空白行,以及將指令分組為彼此相關的小團體。

更重要的是,在條件跳轉之后,插入一條空白行,在絕對跳轉之后,插入兩條空白行。 (老技巧,提高可讀性)

其次,排列評論,使它們排列整齊。 看起來好一千倍。

這是您的資料,我整理了90秒的文字。 相信我,專業人員會以這種源代碼更好地尊重您一千次。

    pushl %ebp              //   What i think is happening here is that we are creating more space for the function.
    movl %esp,%ebp          //   Here i think we are moving the stack pointer to the old base pointer.

    movl 8(%ebp),%edx       //   Here we are taking parameter int a and storing it in %edx
    movl 12(%ebp),%eax      //   Here we are taking parameter int b and storing it in %eax


    cmpl %eax,%edx          //   Here i think we are comparing int a and b ( b > a ) ?
                            //   No, Think like this: "What is the value of edx with respect to the value of eax ?"

    jge .L3                 //   edx is greater, so return the value in eax as it is

    movl %edx,%eax          //   If the term is not met here it will return b
                            //   (pssst, I think you're wrong; think it through again)

    .L3:

    movl %ebp,%esp          //   Starting to finish the function
    popl %ebp               //   Putting the base pointer in the right place
    ret                     //   return

現在,回到您遇到的問題。 他得到的是比較指令和相關的JGE指令的“感覺”。

這是您在此類“學術經歷”中生存所需要理解的混淆性內容

biz, cmpl %eax,%edx指令,是“ compare ”指令的形式之一

當您看到以下語法時,請嘗試形成類似這樣的想法:“ ... 目標操作數相對於源操作數的值是多少? ...”

警告:我絕對不喜歡AT&T語法,因此歡迎任何人對此進行糾正。

無論如何,在這種情況下,您可以像這樣在您的腦海中表達這個想法...

“ ...我看到cmpl %eax,%edx所以我認為: eaxedx的值是 ...”

然后,您可以通過下一條指令的“感覺”(有條件的跳轉)在腦海中完成該句子。

人腦中的范式過程形成了這樣的句子。

“ ... 關於eaxedx的值大於或等於,所以我跳了 ……”

因此,如果您對ab的位置是正確的,那么您可以進行范例性的腦力爭奪者,並得到類似的東西...

“...... 對於在值b ,在價值a大於或等於,所以我會跳 ......”

要了解這一點,請注意,如果您願意, JGEJL是“相反的意思”(即,“小於則跳轉”)

好的,現在碰巧C的return 匯編語言中的ret指令有關 ,但這不是一回事。

當C程序員說“ ... 那個函數返回一個int ...”時,它們的意思是...

  • 匯編語言子例程將在Eax放置一個值
  • 然后,該子例程將修復堆棧並以整齊的順序放回堆棧
  • 然后該子例程將執行其Ret指令

現在,您的臉上又出現了一種混淆。

以下這些條件跳轉適用於有符號算術比較操作...

  • JG
  • JGE
  • JNG
  • JL
  • JLE
  • JNL

那里! 陷阱等着你把這一切搞砸了!

您要進行有符號比較還是無符號比較?

順便說一句,我從未見過有人像第一個函數那樣將無符號數與帶符號數進行比較。 那合法嗎?

所以無論如何,我們把所有這些事實在一起,我們得到: 該匯編語言程序返回的值a ,如果是小於值b否則在返回值b

這些值被評估為有符號整數。

(我認為我沒錯;有人檢查了我的邏輯。我真的根本不喜歡那個匯編程序的語法。)

因此,無論如何,我可以肯定地說,您不想讓互聯網上的人為您提供針對您特定作業問題的特定答案,因此,我將由您自己決定是否從此解釋中找到答案。

希望我已經對比較的邏輯和比較以及已簽名和未簽名的biz進行了充分的解釋,以便您可以解決此問題。

哦,再次免責聲明,我總是使用Intel語法(例如,Masm,Tasm,Nasm等),因此,如果我在這里遇到了一些問題,請隨時為我更正。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM