Linux 汇编-函数调用

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/

table th {
font-weight: bold;
}

table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}

table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}

table tr:nth-child(2n) {
background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

GNU 汇编函数

定义函数

  • 格式
.type func_name, @function
func_name:
#content
ret
  • 注解

.type 指令指定 func_name 为汇编程序调用此函数的地址

fun_name 为函数名

@function 表示函数内容开始

ret 表示函数结束,返回到父函数调用子函数处

调用函数

  • 格式
call fun_name
  • 注解

call 是调用子函数的汇编命令

fun_name 是定义子函数时,.type 指定的函数地址

编写汇编函数

使用寄存器和全局变量传递函数参数

即在主函数中先将数据保存到某寄存器,然后在函数中直接使用这个寄存器的指。

# ASM function use register give the inut data
.section .data
str:.ascii "ASM THIRD DAY\n"
sl=.-str .section .text
.global _start
_start:
movl $0, %eax # Init eax
movl $2, %ebx # Prepare data in ebx first call add_fun # Then call child function # System call: write() movl $1, %ebx
movl $str, %ecx
movl $sl, %edx int $0x80 movl $1, %eax # System call: exit
movl $0, %ebx
int $0x80 # The method of GNU define a function
.type add_fun, @function
add_func:
add %ebx, %ebx
movl %ebx, %eax
ret
root@root:~# as fun_register.s -o fun.o
root@root:~# ld fun.o -o fun
root@root:~# .fun
  • 注解

主程序中,movl $2, %ebx 将常量 2 赋值给寄存器 ebx, 为子函数 add_func 准备好数据。

接下来调用子函数,使用 add 指令对寄存器 ebx 数据进行处理。

通过全局变量给子函数传递数据,只需在数据段(.data 或者 .bss)定义变量,在主函数中初始化后在调用子函数来使用变量。

在父函数调用子函数时,一定要符合子函数的需求:准备好要用到的寄存器和变量,准备好子函数输出结果的保存地址,同时主函数中也要直到函数输出保存的地址。

使用 C 风格传参方式

C 风格传参方式使用栈来给子函数准备数据,适合复杂的汇编程序

只在栈顶发生操作,入栈的新栈顶,出栈移除当前栈顶元素,入栈 push,出栈 pop

堆栈寄存器 esp 需要始终指向栈顶。

ebp 用于保存栈基址。

高地址内存叫栈底,低地址内存叫栈顶。

入栈后,esp 值减小,即指向更小的地址。

除使用 pushpop 指令外,还可操作栈地址来实现。通常将 esp 值拷贝到 ebp 来操作。

  • 通过栈为函数准备数据

将子函数需要的数据存到栈里,需要保证存在栈里的数据与子函数使用的顺序相对应。

注意: 当调用子函数时,系统会将当前地址压入栈中,当子函数执行完之后返回之前地址,以便程序继续执行。

# Given data to child function by stack
.section .data
str:.ascii "ASM FOURTH DAY\n"
sl=.-str .section .text
.global _start
_start:
push $4 # Be ready data for child func by stack
call give_value_to_eax movl $1, %ebx # System call: write()
movl $str, %ecx
movl $sl, %edx int $0x80 movl $1, %eax # System call exit
movl $0, %ebx
int $0x80 # Child func
.type give_value_to_eax, @function
give_value_to_eax:
pushl %ebp # Save ebp value
movl %esp, %ebp # Get stack top address for ebp movl 8(%ebp), %eax # eax's value is the bottom value of the stack pop %ebp # Get the old ebp value again
ret
root@root:~# as fun_stack.s -o fun.o
root@root:~# ld fun.o -o fun
root@root:~# .fun
  • 注解

主函数中,是两个系统调用代码。

write 系统调用代码中,eax 的值通过子函数 give_value_to_eax 来赋予。

在 give_value_to_eax 子函数中,使用在主函数中在栈中准备的数据来给 eax 赋值。因为压入栈中的还有当前调用函数的地址,因此使用 ebp 来访问原先压入栈中的数据。

子函数中,第一行为备份 ebp 值,第二行将栈顶的 esp 值赋给 ebp,第三行通过 ebp 找到主函数为子函数准备的数据,并赋给 eax。

使用 ebp 来获取 esp 值,然后通过 ebp 来访问栈中数据是为了保证 esp 一直指向栈顶。

 Program Stack
+-------------------------+
| |
| | Indirect addresing
|-------------------------|
| Function parameter 3 | 16(%ebp)
|-------------------------|
| Function parameter 2 | 1(%ebp)
|-------------------------|
| Function parameter 1 | 8(%ebp)
|-------------------------|
| Return Address | 4(%ebp)
|-------------------------|
| Old EBP Value | (%ebp) <----- ESP
+-------------------------+

函数末尾 pop %ebp 之后,栈顶就是 Return Address 了。

当要用栈来存储函数内的局部变量时,只需要将 esp 继续开辟新的栈顶增加即可:

 Program Stack
+-------------------------+
| |
| | Indirect addresing
|-------------------------|
| Function parameter 3 | 16(%ebp)
|-------------------------|
| Function parameter 2 | 1(%ebp)
|-------------------------|
| Function parameter 1 | 8(%ebp)
|-------------------------|
| Return Address | 4(%ebp)
|-------------------------|
| Old EBP Value | (%ebp)
|-------------------------|
| Local Variable 1 | -4(%ebp)
|-------------------------|
| Local Variable 2 | -8(%ebp)
|-------------------------|
| Local Variable 3 | -12(%ebp) <---- ESP
|-------------------------|
| |
|-------------------------|
| |
+-------------------------+

linux 汇编 - 函数调用的更多相关文章

  1. linux 汇编函数调用

    edi第一个参数 esi第二个参数 edx第三个参数 rax保存结果 C++代码如下: char* demo(char* a,int b){ static char* buf=0; if(!buf)b ...

  2. x64架构下Linux系统函数调用

    原文链接:https://blog.fanscore.cn/p/27/ 一. 函数调用相关指令 关于栈可以看下我之前的这篇文章x86 CPU与IA-32架构 在开始函数调用约定之前我们需要先了解一下几 ...

  3. Linux下函数调用堆栈帧的详细解释【转】

    转自:http://blog.chinaunix.net/uid-30339363-id-5116170.html 原文地址:Linux下函数调用堆栈帧的详细解释 作者:cssjtuer http:/ ...

  4. Linux汇编与C互相调用

    一.简介 C语言调用汇编有两种方式:1.通过内嵌汇编  2.通过编译链接. 二.基础知识 对于C和汇编语言的接口主要有两个问题需要解决 1.调用者与被调用者的参数传递 正常的,定义一个函数总是希望它完 ...

  5. 源码分析:动态分析 Linux 内核函数调用关系

    源码分析:动态分析 Linux 内核函数调用关系 时间 2015-04-22 23:56:07  泰晓科技 原文  http://www.tinylab.org/source-code-analysi ...

  6. Linux汇编教程01: 基本知识

    在我们开始学习Linux汇编之前,需要简单的了解一下计算机的体系结构.我们不需要特别深入的了解,理解了一些基本概念对与我们理解程序会很有帮助.现在计算机的结构体系都是采用冯诺依曼体系结构的基础上发展过 ...

  7. Windows x64汇编函数调用约定

    最近在写一些字符串函数的优化,用到x64汇编,我也是第一次接触,故跟大家分享一下. x86:又名 x32 ,表示 Intel x86 架构,即 Intel 的32位 80386 汇编指令集. x64: ...

  8. Linux汇编教程02:编写第一个汇编程序

    学习一门语言,最好的方式就是在运用中学习,那么在这一章节中,我们开始编写我们的第一个汇编程序.当然作为第一个程序,其实十分的简单,但可以给大家一个基本的轮廓,了解汇编大概是这样的. 我们这个程序实际上 ...

  9. 64位linux 汇编

    c源码:testg.c 1 #include<stdio.h>                2                                   3 #define s ...

随机推荐

  1. Linux服务管理(Ubuntu服务管理工具sysv-rc-conf)(转)

    Linux运行级别 Linux系统任何时候都运行在一个指定的运行级上,并且不同的运行级的程序和服务都不同,所要完成的工作和要达到的目的都不同,系统可以在这些运行级之间进行切换,以完成不同的工作. 运行 ...

  2. pga_aggregate_target, sga_target, memory_target

    对于这三个参数有一些了解,但是又有一些疑惑. pga_aggregate_target 最初的了解: 这个参数控制着PGA的大小,如果work_area_policy 设置成auto,则oracle采 ...

  3. spring mvc参数校验

    一.在SringMVC中使用 使用注解 1.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-validator-4.2.0.Fina ...

  4. SfM执行流程

    整个过程根据脚本执行过程来分析. 首先我们看到RunBundler.sh,这个shell脚本. 1.定义参数 BASE_PATH="/cygdrive/e/ProjectBefore/Lea ...

  5. Jupyter Notebook: 解决build docker-stacks时conda太慢的问题

    当想使用docker安装Jupyter Notebook时,有一个很好的项目是docker-stacks(https://github.com/jupyter/docker-stacks/tree/m ...

  6. Linux: 通过命令行上传文件到ftp服务器

    url -T fie-name ftp://server-address --user user:password

  7. 一个性能较好的jvm參数配置以及jvm的简单介绍

    一个性能较好的webserverjvm參数配置: -server //服务器模式 -Xmx2g //JVM最大同意分配的堆内存,按需分配 -Xms2g //JVM初始分配的堆内存.一般和Xmx配置成一 ...

  8. wpf 禁用启用webbroswer右键菜单

    //禁用脚本错误等类似的窗口信息 this.webBrowser1.ScriptErrorsSuppressed = true; //禁用右键菜单 this.webBrowser1.IsWebBrow ...

  9. 一款炫酷Loading动画--载入失败

    简单介绍 上一篇文章一款炫酷Loading动画–载入成功.给大家介绍了成功动画的绘制过程,这篇文章将接着介绍载入失败特效的制作. 相比成功动画,有了前面的经验,失败动画的过程就显得比較简单了. 动画结 ...

  10. php 把一个数组分成有n个元素的二维数组的算法

    一.第一种解法 <?php //把一个数组分成几个数组 //$arr 是数组 //$num 是数组的个数 function partition($arr,$num){ //数组的个数 $list ...