`

assembly - file

 
阅读更多

assembly operate file,

 

------

unix/linux file

 

linux use the same file concept of unix, any file could be accessed as a sequential stream of bytes,

 

step to use file:

* open file

      open file by file path, and will get a number called file descriptor, which will be used to refer to the file later,

* read/write file

      then read/write file with file descriptor,

* close file

      when done, close file, and the file descriptor will be released,

 

------

file relative syscalls

 

include:

* open file

      request:

            %eax: syscall number, 5,

            %ebx: address of file name,

            %ecx: open mode

            %edx: permission

      response:

            %eax: file descriptor which >=0, or error number which <0,

* close file

      request:

            %eax: syscall number, 6,

            %ebx: file descriptor,

* read file

      request:

            %eax: syscall number, 3,

            %ebx: file descriptor,

            %ecx: address of buffer

            %edx: size(byte) to read

      response:

            %eax: size(byte) that actural read which >=0 (0 means end of file), or error code which <0,

* write file

      request:

            %eax: syscall number, 3,

            %ebx: file descriptor,

            %ecx: address of buffer

            %edx: size(byte) to write

      response:

            %eax: size(byte) that actural write which >=0, or error code which <0,

 

------

file open mode

 

* 0

      read only

* 03101

      write only, create file if not exist, clear file first if exist,

* ..

 

------

permission

 

same as linux permission, in octal,

e.g.

      0644

      0666

 

------

standard & special files

 

standard file:

      linux has at least 3 standard file opened when started, and their file descriptor is already available, need not to open/close them,

      include:

      * STDIN

            standard input, represent keyboard usually, read only,

            file descriptor = 0,

            use enter to input a line, ctrl+c to stop program, the last line without enter will be ignored,

      * STDOUT

            standard output, represent screen display usually, write only,

            file descriptor = 1,

      * STDERR

            standard error, represent screen display usually, write only, error msg usually go to this file,

            file descriptor = 2,

 

special file:

      linux treat everything as file, include special file which are actural not file,

      include: input/output system, network connection, serial port, pipe that processes communicate with each other, .. ,

      some of special file are open/close by different methods than regular file, but they can be read/write the same as regular file,

 

------

end of file

 

read, return readed byte count into %eax, 0 means end of file,

 

------

code

 

file_copy.s

 

# file copy
# 	
# command line param:
#	first param:
#		input file path
#	second param:
# 		output file path
# 

.section .data
# constants
# stack position
.equ ST_ARGC, 0		# param count
.equ ST_ARGV_0, 4	# program file name
.equ ST_ARGV_1, 8	# input file name
.equ ST_ARGV_2, 12	# output file name
.equ ST_SIZE_RESERVE, 8	# reserve size
.equ ST_FD_IN, -4	# file descriptor - input file
.equ ST_FD_OUT, -8	# file descriptor - output file
# syscall number
.equ SYS_OPEN, 5
.equ SYS_CLOSE, 6
.equ SYS_READ, 3
.equ SYS_WRITE, 4
.equ SYS_EXIT, 1
# file open mode
.equ MODE_RDONLY, 0
.equ MODE_WRONLY_TRUNC, 03101
# syscall interrupt
.equ LINUX_SYSCALL, 0x80
# end of file
.equ END_OF_FILE, 0	# return value of read

.section .bss
.equ BUF_SIZE, 1024	# BUF_DATAfer size in byte
.lcomm BUF_DATA, BUF_SIZE	# a buffer

.section .text
.globl _start

_start:
movl %esp, %ebp	# backup %esp
subl $ST_SIZE_RESERVE, %esp	# reserve stack
jmp file_open

file_open:
open_input:
movl $SYS_OPEN, %eax
movl ST_ARGV_1(%ebp), %ebx
movl $MODE_RDONLY, %ecx
movl $0644, %edx
int $LINUX_SYSCALL
movl %eax, ST_FD_IN(%ebp)
open_output:
movl $SYS_OPEN, %eax
movl ST_ARGV_2(%ebp), %ebx
movl $MODE_WRONLY_TRUNC, %ecx
movl $0644, %edx
int $LINUX_SYSCALL
movl %eax, ST_FD_OUT(%ebp)
jmp file_read

# read/write until end of file
file_read:
movl $SYS_READ, %eax
movl ST_FD_IN(%ebp), %ebx
movl $BUF_DATA, %ecx
movl $BUF_SIZE, %edx
int $LINUX_SYSCALL
cmpl $END_OF_FILE, %eax
jle file_close
jmp file_write

file_write:
movl %eax, %edx
movl $SYS_WRITE, %eax
movl ST_FD_OUT(%ebp), %ebx
int $LINUX_SYSCALL
jmp file_read

file_close:
close_input:
movl $SYS_CLOSE, %eax
movl ST_FD_IN(%ebp), %ebx
int $LINUX_SYSCALL
close_output:
movl $SYS_CLOSE, %eax
movl ST_FD_OUT(%ebp), %ebx
int $LINUX_SYSCALL
movl %ebp, %esp	# restore %esp
jmp exit

exit:
movl $SYS_EXIT, %eax
int $LINUX_SYSCALL
 

file_toupper.s

 

# file to upper case
# 	
# command line param:
#	first param:
#		input file path
#	second param:
# 		output file path
# 

.section .data
# constants
# stack position
.equ ST_ARGC, 0		# param count
.equ ST_ARGV_0, 4	# program file name
.equ ST_ARGV_1, 8	# input file name
.equ ST_ARGV_2, 12	# output file name
.equ ST_SIZE_RESERVE, 8	# reserve size
.equ ST_FD_IN, -4	# file descriptor - input file
.equ ST_FD_OUT, -8	# file descriptor - output file
# syscall number
.equ SYS_OPEN, 5
.equ SYS_CLOSE, 6
.equ SYS_READ, 3
.equ SYS_WRITE, 4
.equ SYS_EXIT, 1
# file open mode
.equ MODE_RDONLY, 0
.equ MODE_WRONLY_TRUNC, 03101
# syscall interrupt
.equ LINUX_SYSCALL, 0x80
# end of file
.equ END_OF_FILE, 0	# return value of read

.section .bss
.equ BUF_SIZE, 1024	# BUF_DATAfer size in byte
.lcomm BUF_DATA, BUF_SIZE	# a buffer

.section .text
.globl _start

_start:
movl %esp, %ebp	# backup %esp
subl $ST_SIZE_RESERVE, %esp	# reserve stack
jmp file_open

file_open:
open_input:
movl $SYS_OPEN, %eax
movl ST_ARGV_1(%ebp), %ebx
movl $MODE_RDONLY, %ecx
movl $0644, %edx
int $LINUX_SYSCALL
movl %eax, ST_FD_IN(%ebp)
open_output:
movl $SYS_OPEN, %eax
movl ST_ARGV_2(%ebp), %ebx
movl $MODE_WRONLY_TRUNC, %ecx
movl $0644, %edx
int $LINUX_SYSCALL
movl %eax, ST_FD_OUT(%ebp)
jmp file_read

# read/write until end of file
file_read:
movl $SYS_READ, %eax
movl ST_FD_IN(%ebp), %ebx
movl $BUF_DATA, %ecx
movl $BUF_SIZE, %edx
int $LINUX_SYSCALL
cmpl $END_OF_FILE, %eax
jle file_close
pushl $BUF_DATA	# param 1
pushl %eax	# backup %eax, as well as param 0
call toupper
popl %eax	# restore %eax
addl $4, %esp	# restore %esp
jmp file_write

file_write:
movl %eax, %edx
movl $SYS_WRITE, %eax
movl ST_FD_OUT(%ebp), %ebx
movl $BUF_DATA, %ecx
int $LINUX_SYSCALL
jmp file_read

file_close:
close_input:
movl $SYS_CLOSE, %eax
movl ST_FD_IN(%ebp), %ebx
int $LINUX_SYSCALL
close_output:
movl $SYS_CLOSE, %eax
movl ST_FD_OUT(%ebp), %ebx
int $LINUX_SYSCALL
movl %ebp, %esp	# restore %esp
jmp exit

exit:
movl $SYS_EXIT, %eax
int $LINUX_SYSCALL

# a function that convert a buffer in memory from lower case to upper case
# param:
#       first param:
#               buffer size, in byte,
#       second param:
#               start address of buffer
# storage:
#       %eax:
#               start address of buffer
#       %ebx:
#               buffer size
#       %cl:
#               store each char in buffer
#       %edi:
#             	current buffer offset
.type toupper, @function
toupper:
.equ LOWER_A, 'a'
.equ LOWER_Z, 'z'
.equ LOWER_TO_UPPER, 'a'-'A'
toupper_begin:
pushl %ebp	# backup %ebp
movl %esp, %ebp
movl 8(%ebp), %ebx	# param 0 - actual buf size
movl 12(%ebp), %eax	# param 1 - buf location
movl $0, %edi		# init %edi for loop
jmp toupper_loop

toupper_loop:
cmpl %edi, %ebx
jle toupper_end
movb (%eax, %edi, 1), %cl	# read 1 byte
cmpb $LOWER_A, %cl
jl toupper_loop_end
cmpb $LOWER_Z, %cl
jg toupper_loop_end
subb $LOWER_TO_UPPER, %cl	# lower to upper
movb %cl, (%eax, %edi, 1)	# change buf
toupper_loop_end:
incl %edi
jmp toupper_loop

toupper_end:
movl %ebp, %esp
popl %ebp
ret

 

file_stdin.s

 

# file - stdin, save input of stdin to a file,
#
# params:
#	first param: output file path
# keys:
# 	enter -> to input a line,
# 	ctrl+c -> to stop program,
# 

.section .data
.equ SYS_EXIT, 1
.equ SYS_READ, 3
.equ SYS_WRITE, 4
.equ SYS_OPEN, 5
.equ SYS_CLOSE, 6
.equ MODE_REONLY, 0
.equ MODE_WTONLY_TRUNC, 03101
.equ FD_STDIN, 0
.equ LINUX_SYSCALL, 0x80
.equ EOF, 0
.equ ST_ARGC, 4		# param count
.equ ST_ARGV_0, 8	# program file path
.equ ST_ARGV_1, 12	# output file path
.equ ST_RESERVE_SIZE, 8
.equ ST_FD_OUTPUT, -4
.equ ST_LAST_READ_SIZE, -8
.section .bss
.equ BUF_SIZE, 100
.lcomm BUF_DATA, BUF_SIZE+1
.section .text
.globl _start

_start:
pushl %ebp
movl %esp, %ebp
subl $ST_RESERVE_SIZE, %esp
jmp file_open

file_open:
movl $SYS_OPEN, %eax
movl ST_ARGV_1(%ebp), %ebx
movl $MODE_WTONLY_TRUNC, %ecx
movl $0644, %edx
int $LINUX_SYSCALL
movl %eax, ST_FD_OUTPUT(%ebp)

read:
movl $SYS_READ, %eax
movl $FD_STDIN, %ebx
movl $BUF_DATA, %ecx
movl $BUF_SIZE, %edx
int $LINUX_SYSCALL
movl %eax, ST_LAST_READ_SIZE(%ebp)

write:
movl $SYS_WRITE, %eax
movl ST_FD_OUTPUT(%ebp), %ebx
movl $BUF_DATA, %ecx
movl ST_LAST_READ_SIZE(%ebp), %edx
int $LINUX_SYSCALL
jmp read

file_close:
movl $SYS_CLOSE, %eax
int $LINUX_SYSCALL

end:
movl %ebp, %esp
popl %ebp
movl $SYS_EXIT, %eax
int $LINUX_SYSCALL

 
file_stdout.s

 

# file - stdout, read file and print to stdout, similar to cat command
#
# params:
#	first param: input file path
# 

.section .data
.equ SYS_EXIT, 1
.equ SYS_READ, 3
.equ SYS_WRITE, 4
.equ SYS_OPEN, 5
.equ SYS_CLOSE, 6
.equ MODE_REONLY, 0
.equ MODE_WTONLY_TRUNC, 03101
.equ FD_STDIN, 0
.equ FD_STDOUT, 1
.equ LINUX_SYSCALL, 0x80
.equ EOF, 0
.equ ST_ARGC, 4		# param count
.equ ST_ARGV_0, 8	# program file path
.equ ST_ARGV_1, 12	# input file path
.equ ST_RESERVE_SIZE, 8
.equ ST_FD_INPUT, -4
.equ ST_LAST_READ_SIZE, -8
.section .bss
.equ BUF_SIZE, 100
.lcomm BUF_DATA, BUF_SIZE
.section .text
.globl _start

_start:
pushl %ebp
movl %esp, %ebp
subl $ST_RESERVE_SIZE, %esp
jmp file_open

file_open:
movl $SYS_OPEN, %eax
movl ST_ARGV_1(%ebp), %ebx
movl $MODE_REONLY, %ecx
movl $0644, %edx
int $LINUX_SYSCALL
movl %eax, ST_FD_INPUT(%ebp)

read:
movl $SYS_READ, %eax
movl ST_FD_INPUT(%ebp), %ebx
movl $BUF_DATA, %ecx
movl $BUF_SIZE, %edx
int $LINUX_SYSCALL
movl %eax, ST_LAST_READ_SIZE(%ebp)

write:
cmpl $0, %eax
jle file_close
movl $SYS_WRITE, %eax
movl $FD_STDOUT, %ebx
movl $BUF_DATA, %ecx
movl ST_LAST_READ_SIZE(%ebp), %edx
int $LINUX_SYSCALL
jmp read

file_close:
movl $SYS_CLOSE, %eax
int $LINUX_SYSCALL

end:
movl %ebp, %esp
popl %ebp
movl $SYS_EXIT, %eax
int $LINUX_SYSCALL
 

------


分享到:
评论

相关推荐

    SpringBoot项目使用maven-assembly-plugin根据不同环境打包成tar.gz或者zip

    首先,`maven-assembly-plugin`允许我们在构建过程中定义多个不同的组装配置(assembly),每个配置可以针对特定的环境。配置文件通常位于项目的`src/main/assembly`目录下,以XML格式编写。例如,我们可以创建一个...

    Maven3种打包方式中maven-assembly-plugin的使用详解

    Maven3种打包方式中maven-assembly-plugin的使用详解 Maven 作为一个流行的项目管理工具,提供了多种方式来打包项目。其中,maven-assembly-plugin 是一个非常强大和灵活的插件,支持自定义的打包结构,并且可以...

    assembly-stats:从FASTA和FASTQ文件获取程序集统计信息

    装配状态从FASTA和FASTQ文件获取程序集统计信息。内容安装如果在安装程序集统计信息时遇到问题,请与本地系统管理员联系。...用法从文件列表中获取统计信息: assembly-stats file.fasta another_file.fastq从

    ksoap2-android-assembly-3.5.0-jar-with-dependencie.jar

    implementation fileTree(dir: 'libs', include: ['*.jar']) } ``` 确保在同步后,该库已经被正确引入。 2. 创建SOAP请求 首先,需要定义SOAP动作、命名空间、服务URL以及请求的方法名。例如: ```java final ...

    maven-assembly 自定义打包方式

    而`maven-assembly-plugin`是Maven的一个插件,用于创建自定义的打包(assembly)格式,比如tar、tar.gz、zip等。这个插件允许开发者在打包过程中包含额外的资源,如配置文件、文档等,使得最终的发布包更完整,便于...

    assembly项目打包插件,包含windows与linux环境下的启动脚本

    set JAR_FILE=&lt;your-jar-file-name&gt;.jar set JAVA_OPTS=-Dspring.profiles.active=&lt;env-profile&gt; -Xms256m -Xmx512m java %JAVA_OPTS% -jar %JAR_FILE% ``` 这里,`JAR_FILE`是你的应用JAR,`JAVA_OPTS`包含了运行时...

    spark-2.3.0-bin-hadoop2.7版本.zip

    Hadoop2.7是Hadoop生态系统中的一个版本,它为Spark提供了分布式存储的基础,即HDFS(Hadoop Distributed File System)。Spark与Hadoop的集成,使得Spark可以无缝地读取和写入Hadoop的数据,进一步增强了其在大数据...

    maven资料Maven Assembly Plugin

    ### Maven Assembly Plugin详解 #### 一、概述 Maven Assembly Plugin 是一个强大的工具,主要用于将项目的输出及其依赖项、模块、站点文档和其他相关文件合并到一个可分发的归档文件中。这对于创建最终可部署的包...

    Assembly-Java:从Java实例调用Assembly

    /* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class NativeDemo */ #ifndef _Included_NativeDemo #define _Included_NativeDemo #ifdef __cplusplus extern "C" { #endif /* ...

    assembly.zip

    在IT行业中,Assembly是一种低级编程语言,它与机器代码紧密相关,允许程序员直接控制硬件资源。在这种情况下,"assembly.zip"是一个包含了与Maven项目相关的Assembly打包过程的文件集合。Maven是一个强大的项目管理...

    使用Maven assembly打多个自定义包及War包简介

    本篇文章将深入探讨如何使用Maven Assembly插件来创建多个自定义包以及War包,这对于Java开发者来说尤其重要,因为它使得项目打包和分发变得更加方便。 Maven是一个强大的Java项目管理工具,它通过配置文件POM...

    maven assembly 插件 代码实现

    ** Maven Assembly 插件详解与实践 ** 在Java开发中,Maven作为一款强大的构建工具,极大地简化了项目管理与构建过程。而Maven Assembly插件则是Maven生态系统中的一个重要组成部分,它允许开发者将项目的所有依赖...

    ksoap2完整版jar包 Android调用webservice

    3. **ksoap2-android-assembly-3.2.0-jar-with-dependencies.jar** 这个jar文件是ksoap2的完整版本,其中包含了所有依赖,避免了因为缺少依赖而引发的运行时错误。在Android Studio项目中,你可以将这个jar文件添加...

    打jar包注意点.docx

    总结来说,通过在`pom.xml`中正确配置`maven-jar-plugin`和`maven-assembly-plugin`,并配合`assembly.xml`文件,可以确保批处理项目被打包成一个可执行的JAR文件,其中包含了所有必要的依赖,并且能够明确指定主类...

    SAE J2286-2015 Vendor Component Program Data File Interface for OEM Assembly Operations - 完整英文版(22页).pdf

    SAE J2286-2015 是一项由国际自动机工程师学会(SAE International)发布的标准,标题为“Vendor Component Program Data File Interface for OEM Assembly Operations”,即供应商组件程序数据文件接口用于原始设备...

    source insight ARM Assembly CLF file

    source insight的arm 汇编语言支持文件,支持标号常量变量指令伪指令着色高亮显示,比在官网上的文件强,需要的下。缺陷是宏定义的定义体及标号不能识别。

    Professional Assembly Language

    ***请支持开源*** 汇编大师Richard Blum的经典AT&T汇编教程英文原版(没有中文版) 下面是书中的介绍 Introduction ... This chapter shows how to use the Linux file-handling system calls to read,...

    spark-cassandra-example:spark cassandra 连接器的使用示例

    /your/path/to/spark/bin/spark-submit --properties-file cassandra-example.conf --class org.koeninger.HelloWorldExample target/scala-2.10/cassandra-example-assembly-0.1-SNAPSHOT.jar 要将 jar 添加到 ...

    osm-loader:OSM数据提取器和索引

    渗透加载器 从pbf文件中提取数据并将其存储在文件夹中(即将推出:到SimplexSpatial服务器中)。... -i, --in &lt;value&gt; input folder or file -o, --out &lt;value&gt; output folder -t, --type &lt;value&gt; export type

    The Art of Assembly Language Programming

    Some Final Comments on the MOV Instructions &lt;br&gt;4.9 Laboratory Exercises 4.9.1 The UCR Standard Library for 80x86 Assembly Language Programmers 4.9.2 Editing Your Source Files 4.9.3 The ...

Global site tag (gtag.js) - Google Analytics