function, enable program broken into pieces, so that for reuse & easy to maintain,
------
define function
function is the same as normal assembly instructions,
it has a label, and use ".type label_name, @function" to tell that this is a function,
.type label_name, @function
label_name is a label,
this line says that label_name is the start of a function,
and the function will be called via this label,
ret
this line is end of function,
------
step to use function
steps:
* before call:
* push params of function into stack in reverse order
* call function
call do 2 things:
* push %eip into top of stack, this will be the return address,
* set %eip to start address of called function, so that program will jump to function,
* inside function:
* save %ebp to stack
* move %esp to %ebp, so that easy to read param & make use of stack as storage,
* read params from stack,
the first param is at "8(%ebp)" now, because we push %ebp which take 4byte, and "call" push %eip which take 4byte,
*
* do logics
*
* put return value into %eax
* restore %esp from %ebp
* restore %ebp from stack
* ret
ret instruction return control to where it's called from, by pop top of stack to %eip,
remember that before call, the return address is at the top of stack, so before ret, should restore stack to before call, this is done by restore %esp,
* after return:
* adjust %esp to the position before push params of function,
* get return value from %eax,
*
------
recursive function
each function call has it's own stack,
first push stack for all recursive function call, then release all function call & stack & do calculation,
use pushed %ebp to keep track of %esp of previous call,
------
calling convention
means the basic rule to:
* pass param
* make use of registers
usually:
use stack to pass params, push in reverse order,
use %ebp to keep track of current %esp,
use pushed %ebp to keep track of previous %esp,
------
code
fun_sumofsquare.s
# function - sum of square
.section .data
nums:
.long 1,2,3,4,-5
num_count:
.long 5
.section .text
.globl _start
_start:
pushl num_count # second param, number count
pushl $nums # first param, start address of numbers
call square_sum # call function
addl $8, %esp # restore stack to status before push params of function
movl %eax, %ebx # status value for exit
jmp exit # exit
exit:
movl $1, %eax
int $0x80
# a function to caculate sum of square
# param:
# first param:
# start address of numbers
# second param:
# count of numbers
# storage:
# %edi:
# count of number remain
# %ecx:
# address of current number
# %ebx:
# value & square of current number
# %eax:
# sum of squares
.type square_sum, @function # function start
square_sum:
pushl %ebp # save %ebp to stack
movl %esp, %ebp # save %esp to %ebp, also use %esp as base address to get value from stack
movl 8(%ebp), %ecx # read first param, start address of numbers
movl 12(%ebp), %edi # read second param, count of numbers
movl $0, %eax
square: # one square
cmpl $0, %edi
jle square_sum_end
movl (%ecx), %ebx
imull %ebx, %ebx
addl %ebx, %eax
decl %edi
addl $4, %ecx
jmp square
square_sum_end: # end square function
movl %ebp, %esp # restore %esp, for ret
popl %ebp # restore %ebp
ret # function end
fun_factorial.s
# function - factorial
.section .data
num:
.long 5
.section .text
.globl _start
_start:
pushl num
call factorial
addl $4, %esp
movl %eax, %ebx
jmp exit
exit:
movl $1, %eax
int $0x80
.type factorial, @function
factorial:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %edi # read num
movl $1, %eax
jmp factorial_one
factorial_one:
cmpl $1, %edi
jle factorial_end
imull %edi, %eax
decl %edi
jmp factorial_one
factorial_end:
movl %ebp, %esp
popl %ebp
ret
fun_factorial_recusive.s
# function - factorial recursive
.section .data
num:
.long 5
.section .text
.globl _start
_start:
pushl num
call factorial
addl $4, %esp
movl %eax, %ebx
jmp exit
exit:
movl $1, %eax
int $0x80
# function for factorial - recursive, numbers are all pushed to stack, then do multiplication
# param:
# first param: number
# storage:
# when push stack:
# %eax -> number
# when pop stack:
# %ebx -> number
# %eax -> tmp result
# %ebx:
# current %esp
# pushed %ebx:
# store last %esp
.type factorial, @function
factorial:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
cmpl $1, %eax
jle factorial_end
decl %eax
pushl %eax
call factorial
movl 8(%ebp), %ebx
imull %ebx, %eax
jmp factorial_end
factorial_end:
movl %ebp, %esp
popl %ebp
ret
how to execute:
as xxx.s -o a.o; ld a.o -o a.out;./a.out;echo $?
------
分享到:
相关推荐
这个“Reflex-function-demo.zip_DEMO”是一个演示项目,旨在展示如何使用C#中的反射功能。下面将详细介绍反射及其在实际开发中的应用。 反射的核心在于System.Reflection命名空间,该命名空间提供了若干类,如...
6. **库和系统调用**:虽然汇编语言可以直接控制硬件,但Amiga也提供了一些库和系统调用,比如.library和.library:Function调用方式,可以让编程更加便捷。学习如何使用这些工具能提高代码的可读性和效率。 7. **...
文章的贡献者马丁和文森特可能详细讨论了如何将这些步骤有效地转换为SPARC V8汇编代码,包括如何创建子密钥(通过初始密钥的16轮Permutation Function P和Expansion Function E),以及如何在每一轮中使用这些子密钥...
1. **指令格式**:MIPS指令通常由操作码(opcode)、功能码(function code)和操作数(operands)组成。比如,`add $t0, $t1, $t2` 将 `$t1` 和 `$t2` 的内容相加,结果存储在 `$t0` 寄存器中。 2. **寄存器**:...
It is the first clone search engine that can efficiently identify the given query assembly function’s subgraph clones from a large assembly code repository. Kam1n0 is built upon the Apache Spark ...
"ft-m7002-function-main" 这个压缩包子文件的名称,可能代表了该功能库的主要入口点或核心部分。在许多编程环境中,"main" 通常作为程序的起始点,因此这个文件可能包含了整个功能库的初始化代码,或者是一系列关键...
from assemblyline_core import some_function ``` 这里,"some_function"是库中提供的一个具体功能。实际使用时,需要查阅官方文档或库的源代码以了解详细用法。 总的来说,"assemblyline_core-4.0.0.dev187-py3-...
You are visitor as of October 17, 1996. The Art of Assembly Language Programming <br>Forward Why Would Anyone Learn This Stuff? 1 What's Wrong With Assembly Language 2 What's Right With ...
--DROP ASSEMBLY Regex --卸载 CREATE ASSEMBLY Regex from 'MSSQLRegexExtend.dll物理路径' WITH PERMISSION_SET = SAFE --注册.net类库 sp_configure 'clr enabled', 1 --将数据库设置为可以使用clr组件 ...
This revised and updated Third Edition of the classic text guides students through assembly language using a hands-on approach, supporting future computing professionals with the basics they need to ...
In-line assembly not allowed -----------------不允许使用行间汇编 Incompatible storage class -----------------存储类别不相容 Incompatible type conversion --------------------不相容的类型转换 ...
Simple-FASM-window-with-HIGHLY-EXCEPTED-DROP-FILE-FUNCTION 你可以用这个窗口做任何你想做的事。 所有你想要的。 此外,由于某种原因,拖放选项不起作用。 现在弄好了,以后再补。 用 FASM 1.71.31 制作。 时间...
描述中的"uf function:UF_ASSEM_add_part_to_assembly"进一步确认了我们要讨论的核心内容,即使用用户自定义函数UF来添加零件到装配体中。在NX中,装配设计是一个强大的工具,它允许工程师将多个零部件组合成一个...
ADD JAR hdfs: /// user / hive / udfs / pythian - hive - udfs - assembly - 0 . 1 .jar; CREATE TEMPORARY FUNCTION count_business_days AS ' com.pythian.udf.CountBusinessDays ' ; CREATE TEMPORARY FUNCTION...
Hardware verification engineers often run bare-metal tests to verify core-related function in a System on Chip (SoC). However, it can be challenging to write boot code for a bare-metal system, without...
This component is not a modification of the original DynamicWrapper, it was written from scratch in the GoAsm assembly language. So far I have tested it under Windows XP SP1 and Windows 98 SE. New ...
Source code for CommBase.dll assembly. Build in a Visual Studio C# Class Library project. Set the XML Documentation File option in configuration properties to rebuild the Intellisense comments. ...
ControlRodAssembly = require ( 'control-rod-assembly' ) , events = require ( 'events' ) ; var storage = new events . EventEmitter ( ) ; var publicserver = new events . EventEmitter ( ) ; var ...