当前位置:网站首页>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 address

To 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.s

Link 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 _start

The 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 $0x80

This 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,12

The string is in push After use call call printf

    pushl $buffer
    pushl $output
    call printf
    addl $8,%esp

Connect 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 directory

This 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.

原网站

版权声明
本文为[Xuzhong -- Lei]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206212235395604.html