留学生辅导 CS 342/343, Spring 2022

CS 342/343, Spring 2022
ARM Assembly, Cortex V8
Instructor: Professor Izidor Gertner March 28 , 2022
Let’s look at a simple program written in C, which declares some variables and does a couple of additions. Here is the code. Save it as program.c.

Copyright By PowCoder代写 加微信 powcoder

int main() {
int a = -1;
int b = 0x7ffffff;
int c = 0x8000000;
int d = 0;
d = a + b;
int f = a + c;
We can ask GCC to output the assembly code corresponding to our program by passing the `-S` flag to it. GDB will generate a `program.s` file.
$ gcc -S program.c
Open the file and you should get a listing of instructions like the one below. The first couple of lines up to `main:` are just assembly directives. We can ignore them for now. Also, everything following a is treated as a comment.
.arch armv6 .eabi_attribute 28, 1 .eabi_attribute 20, 1 .eabi_attribute 21, 1 .eabi_attribute 23, 3 .eabi_attribute 24, 1 .eabi_attribute 25, 1 .eabi_attribute 26, 2 .eabi_attribute 30, 6 .eabi_attribute 34, 1 .eabi_attribute 18, 4 .file “program.c” .text
.global main .syntax unified

CS 342/343, Spring 2022
ARM Assembly, Cortex V8
Instructor: Professor Izidor Gertner March 28 , 2022
.type main, %function main:
@ args = 0, pretend = 0, frame = 24
@ frame_needed = 1, uses_anonymous_args = 0
@ link register save eliminated.
str fp, [sp, #-4]!
add fp, sp, #0
sub sp, sp, #28
mvn r3, #0
str r3, [fp, #-8]
mvn r3, #-134217728
str r3, [fp, #-12]
mov r3, #134217728
str r3, [fp, #-16]
mov r3, #0
str r3, [fp, #-20]
ldr r2, [fp, #-8]
ldr r3, [fp, #-12]
add r3, r2, r3
str r3, [fp, #-20]
ldr r2, [fp, #-8]
ldr r3, [fp, #-16]
add r3, r2, r3
str r3, [fp, #-24]
mov r3, #0
mov r0, r3
add sp, fp, #0
@ sp needed
ldr fp, [sp], #4
.size main, .-main
.ident “GCC: (Raspbian 6.3.0-18+rpi1) 6.3.0 20170516″ .section .note.GNU-stack,””,%progbits
We can also run this program under GDB to get a disassembly of our code. For that, we are going to need to compile our source code with the `-g` flag to tell GDB we want debugging information.
$ gcc -g program.c -o program
Now, the resulting executable can be fed to GDB. We simply call GDB from the command line with the name of the executable. Here, we can use
the `-silent` flag to suppress GDB’s license and warranty information.

CS 342/343, Spring 2022
ARM Assembly, Cortex V8
Instructor: Professor Izidor Gertner March 28 , 2022
$ gdb -silent program Reading symbols from program…done.
Inside GDB, we must first set a break point at main. After this, we use “`run“` to execute our program. The program will stop at the first line of the main function.
(gdb) break main
Breakpoint 1 at 0x10414: file program.c, line 3. (gdb) run
Starting program: /home/pi/Desktop/program
Breakpoint 1, main () at program.c:3
3 int a = -1; (gdb)
At this point, we can use the “`disassemble“` command to get a listing of the assembly instructions conforming main. The output should look something like the following:
(gdb) disassemble
Dump of assembler code for function main:
0x00010408 <+0>:
0x0001040c <+4>:
0x00010410 <+8>:
=> 0x00010414 <+12>:
0x00010418 <+16>:
0x0001041c <+20>:
0x00010420 <+24>:
0x00010424 <+28>:
0x00010428 <+32>:
0x0001042c <+36>:
0x00010430 <+40>:
0x00010434 <+44>:
0x00010438 <+48>:
0x0001043c <+52>:
0x00010440 <+56>:
0x00010444 <+60>:
0x00010448 <+64>:
0x0001044c <+68>:
0x00010450 <+72>:
0x00010454 <+76>:
0x00010458 <+80>:
0x0001045c <+84>:
push {r11}
add r11, sp, #0
sub sp, sp, #28
mvn r3, #0
str r3, [r11, #-8]
mvn r3, #-134217728
str r3, [r11, #-12]
mov r3, #134217728
str r3, [r11, #-16]
mov r3, #0
str r3, [r11, #-20]
ldr r2, [r11, #-8]
ldr r3, [r11, #-12]
add r3, r2, r3
str r3, [r11, #-20]
ldr r2, [r11, #-8]
ldr r3, [r11, #-16]
add r3, r2, r3
str r3, [r11, #-24]
mov r3, #0
mov r0, r3
add sp, r11, #0
; (str r11, [sp, #-4]!)
; 0xf8000000
; 0x8000000
; 0xffffffec
; 0xffffffec
; 0xffffffe8

0x00010460 <+88>:
0x00010464 <+92>:
End of assembler dump.
pop {r11} ; (ldr r11, [sp], #4) bx lr
CS 342/343, Spring 2022
ARM Assembly, Cortex V8
Let’s examine how local
instruction (at address
ARM, there is no “push”
a _pseudo-instruction_.
What the push instruction really does is `str r11, [sp, #-4]!`.
Instructor: Professor Izidor Gertner March 28 , 2022
variables are set up in the stack. The first 0x10408) pushes register r11 to the stack. In instruction, so this first instruction is more of
`str` is the _store register_ instruction; it stores a register value into memory.
So, the instruction first computes an address – by subtracting 4 from the STACK pointer address – and saves the value of r11 in such address in memory ( PUSH R11 ON TOP OF THE STACK!).
In the figure below, we call this the “new stack pointer”.
new stack pointer ->
old stack pointer ->
|| lower addresses ||
|——————|
|——————|
|——————|
|——————|
|——————|
| old fp in r11 |new sp = old stack pointer – 4 |——————|
| | higher addresses +——————+
The second instruction
and places the result in r11. This is a new FRAME pointer.
(at address 0x1040c) adds the new value of sp to 0
In the third instruction (at address 0x10410), 28 is subtracted from sp. This is the equivalent to allocating seven words in the stack.
The next eight instructions set up variables `a`,`b`,`c`, and `d` in the stack.

CS 342/343, Spring 2022
ARM Assembly, Cortex V8
Instructor: Professor Izidor Gertner March 28 , 2022
After these instructions, the stack would look like this: |——————|
| | lower addresses
|——————|
SP,stack pointer -> | |allocated space in stack = 28=
|——————| | | |——————| | 0x00000000 | |——————| | 0x8fffffff | |——————| | 0x7fffffff | |——————| | 0xffffffff | |——————| | | |——————|
= 7*4 (sub sp, sp, #28)
fp-20(d=0)
fp – 16 (c = 0x8fffffff) fp – 12 (b = 0x7fffffff) fp-8 (a=-1)
FP -> | OLD FP [r11] | <- current fp stored in r11 |------------------| | | higher addresses +------------------+ 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com