第17部分- Linux ARM汇编 交互的示例

第17部分- Linux ARM汇编 交互的示例

我们先来看个交互的示例。

 32位

.data
/* First message */
.balign 4
message1: .asciz "Hey, type a number: "
 
/* Second message */
.balign 4
message2: .asciz "I read the number %d\n"
 
/* Format pattern for scanf */
.balign 4
scan_pattern : .asciz "%d"
 
/* Where scanf will store the number read */
.balign 4
number_read: .word 0
 
.balign 4
return: .word 0
 
.text
 
.global main
main:
    ldr r1, address_of_return        /* r1 ← &address_of_return */
    str lr, [r1]                     /* *r1 ← lr */
 
    ldr r0, address_of_message1      /* r0 ← &message1 */
    bl printf                        /* call to printf */
 
    ldr r0, address_of_scan_pattern  /* r0 ← &scan_pattern */
    ldr r1, address_of_number_read   /* r1 ← &number_read */
    bl scanf                         /* call to scanf */
 
    ldr r0, address_of_message2      /* r0 ← &message2 */
    ldr r1, address_of_number_read   /* r1 ← &number_read */
    ldr r1, [r1]                     /* r1 ← *r1 */
    bl printf                        /* call to printf */
 
    ldr r0, address_of_number_read   /* r0 ← &number_read */
    ldr r0, [r0]                     /* r0 ← *r0 */
 
    ldr lr, address_of_return        /* lr ← &address_of_return */
    ldr lr, [lr]                     /* lr ← *lr */
    bx lr                            /* return from main using lr */
address_of_message1 : .word message1
address_of_message2 : .word message2
address_of_scan_pattern : .word scan_pattern
address_of_number_read : .word number_read
address_of_return : .word return
 
/* External */
.global printf
.global scanf

as -o print.o print.s
gcc -o print print.o

再来看个64位的。

 64位

.data
.balign 4
message1: .asciz "Hey, type a number: "
.balign 4
message2: .asciz "I read the number %d\n"
/* Format pattern for scanf */
.balign 4
scan_pattern : .asciz "%d"
/* Where scanf will store the number read */
.balign 4
number_read: .word 0
.balign 4
return: .word 0
 
.arch armv8-a
.global _start
.text
_start: 
    ldr x0, address_of_message1      /* r0 ← &message1 */
    bl printf                        /* call to printf */
 
    ldr x0, address_of_scan_pattern  /* r0 ← &scan_pattern */
    ldr x1, address_of_number_read   /* r1 ← &number_read */
    bl scanf                         /* call to scanf */
 
    ldr x0, address_of_message2      /* r0 ← &message2 */
    ldr x1, address_of_number_read   /* r1 ← &number_read */
    ldr x1, [x1]                     /* r1 ← *r1 */
    bl printf                        /* call to printf */
 
    ldr x0, address_of_number_read   /* r0 ← &number_read */
    ldr x0, [x0]                     /* r0 ← *r0 */
     	mov x8, 93
	svc 0

address_of_message1 : .dword message1
address_of_message2 : .dword message2
address_of_scan_pattern : .dword scan_pattern
address_of_number_read : .dword number_read
address_of_return : .dword return
 
/* External */
.global printf
.global scanf

编译:

as -g -o print64.o print64.s

ld -o print64 print64.o -lc -I /lib64/ld-linux-aarch64.so.1

执行如下:

$./print64

Hey, type a number: 256

I read the number 256

你可能感兴趣的:(64位,ARM处理器汇编技术系列)