当前位置:网站首页>Assembly language example
Assembly language example
2022-06-22 00:36:00 【Xuzhong -- Lei】
1、 The composition of the program ( Define segments and starting points )
Assembly language consists of defined segments , The three common paragraphs are as follows :
Data segment 、bss paragraph 、 Text segment
All assembly languages must have text , Here is where the executable program declares the script . Data segments and bss Segment is optional . A data segment declares a data element with an initial value . These data are used as variables in assembly language programs .bss Segment declarations are data segments that are initialized with zero values , These elements are often used as buffers for assembly languages
1) Definition segment

bss Segments always precede text segments , The data segment can be moved after the text segment
2) Definition The starting point
When the assembly language is compiled into an executable program , The program must know the starting point
[email protected]:~/ass$ ld main
ld: warning: cannot find entry symbol _start; not setting start addressTo solve this problem ,_start The tag indicates that the program instruction should start from here , If you can't find this tag , The connector will try to find another starting point , But we can't find a starting point for complex programs .
.globl Command declares a label that an external program can access . If you write an external assembly language or c A set of tools for language use , You should use global The command declares the segment label of each function
2、 Create simple programs
1)cpuid
cpuid Is an assembly language instruction , It is not easy to execute it from a high-level language .

cpuid Use a single register value as input ,EAX register ( An accumulator for operands and results ) Used to decide cpuid What information does the instruction generate . according to EAX Register value ,cpuid Instruction in EBX、ECX、EDX Registers generate different information about the processor .

EBX Contains a minimum of four bytes
EDX Contains the middle four bytes
ECX Contains up to four bytes

The example program obtains the information of the register value , And display it to the user according to the readable information .
2) Example program
Write assembler
# cpuid.s extract the the processor vendor ID
.section .data
output:
.ascii "the processor vendor ID is 'xxxxxxxxxxxx'\n"
.section .text
.global _start
_start:
nop
movl $0,%eax
cpuid
movl $output, %edi
movl %ebx,28(%edi)
movl %edx,32(%edi)
movl %ecx,36(%edi)
movl $4,%eax
movl $1,%ebx
movl $output,%ecx
movl $42,%edx
int $0x80
mov $1,%eax
mov $0,%ebx
int $0x80
Generate target file
as -o cpuid.o cpuid.sLink generation binary
ld -o cpuid cpuid.o
Execute binary
[email protected]:~/ass$ ./cpuid
the processor vendor ID is 'GenuineIntel'Program analysis :
Make a statement ascii Code string
output:
.ascii "the processor vendor ID is 'xxxxxxxxxxxx'\n"Declare script segments and start tags
.section .text
.global _startThe first thing in the program is to eax load 0 value , Get vendor id, After loading, the phone must respond to instructions scattered in three output registers :
movl $output, %edi
movl %ebx,28(%edi)
movl %edx,32(%edi)
movl %ecx,36(%edi)The first instruction creates a pointer , Handle in memory declarations output Variables use this pointer ,output The location of the tag will be loaded into EDI In the register . Next, follow EDI The pointer , Including manufacturers ID The contents of the three registers of the string fragment are placed in the correct location in the data memory . Outside the brackets, it means output Data location
In fact, that is
"the processor vendor ID is 'xxxxxxxxxxxx'\n"xx The starting position is 28,xx Yes 12 Bytes , Then he put eax、ebx、ecx Of 12 Two bytes into xxx Inside , Then the output
After putting the bytes , You can output the display information
movl $4,%eax
movl $1,%ebx
movl $output,%ecx
movl $42,%edx
int $0x80This program is actually to put output Write to standard output
This program uses a linux system call .linux The kernel provides many built-in functions that are easy to access from assembly applications , To access these kernel functions , You must use script , It is generated with 0x80 Software interruption of . The execution of specific functions is performed by EAX It's worth it . Without this kernel function , It must be Send to the correct display io The earth .
write Function call parameter
eax Contains the value of the system call
ebx Write file descriptor
ecx Start of string edx Included length
3、 Debugging program
[email protected]:~/ass$ as -gstabs -o cpuid.o cpuid.s
[email protected]:~/ass$ ld -o cpuid cpuid.o
[email protected]:~/ass$ gdb cpuid
We found that there was one more time to build the program -gstabs Instructions
Breaking point

When we get here ok 了
4、 Use... In assembly c function
Use printf
# cpuid2.s view the CPUID vendor id string using C library calls
.code32
.section .data
output:
.asciz "the processor vendor id is '%s'\n"
.section .bss
.lcomm buffer,12
.section .text
.globl _start
_start:
movl $0,%eax
cpuid
movl $buffer,%edi
movl %ebx,(%edi)
movl %edx,4(%edi)
movl %ecx,8(%edi)
pushl $buffer
pushl $output
call printf
addl $8,%esp
pushl $0
call exit
printf Use to enter multiple parameters , These parameters depend on the variables to be displayed , The first variable is the string to output , Appropriate code with display variables
output:
.asciz "the processor vendor id is '%s'\n"Note that asciz instead of ascii, because printf To output a string that ends with a null character .
Because there is no need to define the value of the buffer , So use .lcomm The order declares for him 12 A buffer of bytes
.section .bss
.lcomm buffer,12The string is in push After use call call printf
pushl $buffer
pushl $output
call printf
addl $8,%espConnect c Library function
[email protected]:~/ass$ ld cpuid2 cpuid2.o
ld: cannot find cpuid2: No such file or directory
[email protected]:~/ass$ ld -o cpuid2 cpuid2.o
ld: cpuid2.o: in function `_start':
(.text+0x1f): undefined reference to `printf'
ld: (.text+0x29): undefined reference to `exit'
[email protected]:~/ass$
Will find c Incorrect library connection
ld -o cpuid2 -lc cpuid2.o Access dynamics c The connection after the library is through , But it is still not implemented correctly
[email protected]:~/ass$ ./cpuid2
bash: ./cpuid2: No such file or directoryThis is because parsing c function , But the function itself is not included in the executable In the program . The connector needs to find the necessary library files when running , Obviously this is not the case .

It's old here ,ubuntu This library has not been found , But if you know the principle, don't do experiments
5. summary
A data segment contains data referenced in a specific memory location .bss Section contains uninitialized data elements , Such as work buffer , In the example buffer buffer
Pay attention to the starting point after the template is set
In order to use c function , Must be connected to the host computer c Library connection , To do this better , We learned new commands dynamic-linker.
边栏推荐
- 数据魔术师告诉你整数规划COPT5.0离CPLEX还有多远?
- 【golang】Cannot convert expression of type ‘interface{}‘ to type ‘string‘(解决方案)
- All kinds of FPN in object detection
- rabbit:do_run_postlaunch_phase/0 line 932
- [taro] the solution of taro wechat applet input focusing the cursor incorrectly on the Apple phone
- [安洵杯 2019]吹着贝斯扫二维码
- 你有一个机会,这里有一个舞台
- 火线沙龙第26期-多云安全专场
- Win10使用用户初始密码,连接Win Server失败
- qt QMediaPlayer获取音频播放结束状态
猜你喜欢

Npdp| how to do well in product life cycle management?
![[GXYCTF2019]SXMgdGhpcyBiYXNlPw==](/img/4d/f37968ce8ad1cd8112651e4215f464.png)
[GXYCTF2019]SXMgdGhpcyBiYXNlPw==

Hotline salon issue 26 - cloud security session

American tourist visa interview instructions, let me focus!

数据工程系列精讲(第三讲): Data-centric AI 之特征工程

Katalon recoder common commands
![Xshell can only input the public key solution [personal test] when connecting to the virtual machine](/img/a9/2c315f92cef46976353b52fb1d0fba.png)
Xshell can only input the public key solution [personal test] when connecting to the virtual machine

笔记

美国旅游签证面试须知,我来敲重点啦!

buuctf misc 弱口令
随机推荐
【node】node使用mysql连接池
metersphere与jenkins的持续集成
buuctf misc 间谍启示录
[wechat applet] some pitfalls and precautions of wechat applet using the form
Appium gets the exception of displaying spaces in the middle of object text through XPath
Have you stepped on the 8 most common SQL grammars at work?
数据魔术师告诉你整数规划COPT5.0离CPLEX还有多远?
How does the system integration project management engineer (soft exam intermediate) prepare for the exam?
关于一次Web线下面试的思考
[GXYCTF2019]SXMgdGhpcyBiYXNlPw==
All kinds of FPN in object detection
以父之名活动攻略(可以薅羊毛啦)
Im instant messaging source code + software +app with detailed package video building tutorial
数学知识:最大公约数—约数
Pseudo instruction in arm assembly
【剑指Offer】43. 1~n 整数中 1 出现的次数
8 种最坑SQL语法,工作中踩过吗?
Store API memo
note
Introduction to some code static checking tools