2017-05-03 35 views
0

如何查找彙編程序函數中給定參數和返回值的類型?彙編程序參數類型

lea 0x1(%ecx),%ebx究竟做了什麼? 0x1(%ecx)的地址是否存儲在%ebx

我有一個任務來將C彙編代碼重寫爲C函數,這是我不明白的。我認爲參數是int,因爲mov 0x8(%esp),%edx,mov 0xc(%esp),%eax和返回類型是int以及,但這可能是錯誤的,因爲我們的驗證系統不接受我的解決方案。

整個代碼用於x86,32位GNU/Linux。

我認爲的mov語法是一種mov <target>, <source>但隨後有mov $0x0,%edi,我想,這個目標不能爲0。同樣的,sub $0x18,%esp

這是由toplevel_fnc

080aab5c <subroutine_fnc>: 
80aab5c: 53      push %ebx        // backup 
80aab5d: 8b 54 24 08    mov 0x8(%esp),%edx     // 1st parameter pointer to the memory 
80aab61: 8b 44 24 0c    mov 0xc(%esp),%eax     // 2nd parameter int 
80aab65: 83 f8 09    cmp $0x9,%eax      // if (eax == 9) 
80aab68: 74 15     je  80aab7f <subroutine_fnc+0x23> // true 
80aab6a: 83 f8 20    cmp $0x20,%eax      // else if(eax == 32) 
80aab6d: 74 10     je  80aab7f <subroutine_fnc+0x23> // true 
80aab6f: 3b 05 80 a9 0c 08  cmp 0x80ca980,%eax     // else if(eax == 135047552) 
80aab75: 74 08     je  80aab7f <subroutine_fnc+0x23> // true 
80aab77: c7 02 00 00 00 00  movl $0x0,(%edx)      // else edx = 0 -- first element in stack 
80aab7d: eb 0e     jmp 80aab8d <subroutine_fnc+0x31> // jump to end programm 
80aab7f: 8b 0a     mov (%edx),%ecx      // ecx = edx 
80aab81: 8d 59 01    lea 0x1(%ecx),%ebx     // ebx = &(ecx + 1) 
80aab84: 89 1a     mov %ebx,(%edx)      // edx = ebx 
80aab86: 83 f9 01    cmp $0x1,%ecx      // if(ecx == 1) 
80aab89: 19 d2     sbb %edx,%edx      // edx = 0 and CF is set 
80aab8b: 21 d0     and %edx,%eax      // 
80aab8d: 5b      pop %ebx 
80aab8e: c3      ret 

調用的函數這是toplevel_fnc

080aab8f <toplevel_fnc>: 
80aab8f: 55      push %ebp 
80aab90: 57      push %edi 
80aab91: 56      push %esi 
80aab92: 53      push %ebx 
80aab93: 83 ec 18    sub $0x18,%esp     //24 byte for local variables 
80aab96: c7 44 24 14 00 00 00 movl $0x0,0x14(%esp)    //esp + 0x14 = 0 
80aab9d: 00 
80aab9e: bf 00 00 00 00   mov $0x0,%edi     //edi = 0 
80aaba3: 8d 74 24 13    lea 0x13(%esp),%esi 
80aaba7: 8d 6c 24 14    lea 0x14(%esp),%ebp 
80aabab: eb 2e     jmp 80aabdb <toplevel_fnc+0x4c> 
80aabad: 0f be 44 24 13   movsbl 0x13(%esp),%eax 
80aabb2: 89 44 24 04    mov %eax,0x4(%esp) 
80aabb6: 89 2c 24    mov %ebp,(%esp) 
80aabb9: e8 9e ff ff ff   call 80aab5c <subroutine_fnc> 
80aabbe: 88 44 24 13    mov %al,0x13(%esp) 
80aabc2: 84 c0     test %al,%al 
80aabc4: 74 12     je  80aabd8 <toplevel_fnc+0x49> 
80aabc6: ba 01 00 00 00   mov $0x1,%edx 
80aabcb: 89 d3     mov %edx,%ebx 
80aabcd: 89 f1     mov %esi,%ecx 
80aabcf: b8 04 00 00 00   mov $0x4,%eax 
80aabd4: cd 80     int $0x80 
80aabd6: eb 03     jmp 80aabdb <toplevel_fnc+0x4c> 
80aabd8: 83 c7 01    add $0x1,%edi 
80aabdb: ba 01 00 00 00   mov $0x1,%edx 
80aabe0: bb 00 00 00 00   mov $0x0,%ebx 
80aabe5: 89 f1     mov %esi,%ecx 
80aabe7: b8 03 00 00 00   mov $0x3,%eax 
80aabec: cd 80     int $0x80 
80aabee: 83 f8 01    cmp $0x1,%eax 
80aabf1: 74 ba     je  80aabad <toplevel_fnc+0x1e> 
80aabf3: 89 f8     mov %edi,%eax 
80aabf5: 83 c4 18    add $0x18,%esp 
80aabf8: 5b      pop %ebx 
80aabf9: 5e      pop %esi 
80aabfa: 5f      pop %edi 
80aabfb: 5d      pop %ebp 
80aabfc: c3      ret  

預先感謝您!

+0

'lea 0x1(%ecx),%ebx'等價於將_ECX_中的值加1並將結果存儲在_EBX_中。它不會改變任何標誌。 –

+0

通過'0x8(%esp),%edx'加載的第一個參數實際上是一個指向內存的指針。你可以告訴它,因爲稍後我們會使用像'movl $ 0x0,(%edx)'這樣的指令訪問該內存地址處的數據。 –

+1

'cmp 0x80ca980,%eax'不會做你認爲它的工作。注意它的前面沒有'$'標誌。這意味着0x80ca980是一個內存操作數。因此該指令實際上將內存中地址0x80ca980的4個字節(32位)值與_EAX_中的32位值進行比較。 –

回答

0

我如何知道彙編函數中給定參數和返回值的類型是什麼?

您只能通過查看用於在R0(又名EAX)中存儲返回值的指令並查看存儲在那裏的數據來做出有根據的猜測。彙編語言是無類型的。

lea 0x1(%ecx),%ebx究竟幹什麼? %ebx中是否存儲地址0x1(%ecx)?

它存儲ecx + 1在ebx中指定的地址。對於這些操作數,它實際上是ebx = ecx + 1。

+0

謝謝你的回答,所以事實上,沒有辦法確定它的確定嗎?我的意思是沒有猜測。 – Delfi

+0

它是受過教育的猜測和調試。 – user3344003