mirror of
https://github.com/pkivolowitz/asm_book.git
synced 2026-06-23 15:36:45 +08:00
thank you Marc G.
This commit is contained in:
parent
fedf9aef57
commit
36e97c78f7
1 changed files with 99 additions and 99 deletions
|
|
@ -187,25 +187,25 @@ Here is a hand translation of the above `C` code for function `Sum()`:
|
||||||
.global Sum // 1
|
.global Sum // 1
|
||||||
.text // 2
|
.text // 2
|
||||||
.align 4 // 3
|
.align 4 // 3
|
||||||
// 4
|
|
||||||
// x0 is the pointer to data // 5
|
// x0 is the pointer to data // 5
|
||||||
// x1 is the length and is reused as `end` // 6
|
// x1 is the length and is reused as `end` // 6
|
||||||
// x2 is the sum // 7
|
// x2 is the sum // 7
|
||||||
// x3 is the current dereferenced value // 8
|
// x3 is the current dereferenced value // 8
|
||||||
// 9
|
|
||||||
Sum: // 10
|
Sum: // 10
|
||||||
mov x2, xzr // 11
|
mov x2, xzr // 11
|
||||||
add x1, x0, x1, lsl 3 // 12
|
add x1, x0, x1, lsl 3 // 12
|
||||||
b 2f // 13
|
b 2f // 13
|
||||||
// 14
|
|
||||||
1: ldr x3, [x0], 8 // 15
|
1: ldr x3, [x0], 8 // 15
|
||||||
add x2, x2, x3 // 16
|
add x2, x2, x3 // 16
|
||||||
2: cmp x0, x1 // 17
|
2: cmp x0, x1 // 17
|
||||||
blt 1b // 18
|
blt 1b // 18
|
||||||
// 19
|
|
||||||
mov x0, x2 // 20
|
mov x0, x2 // 20
|
||||||
ret // 21
|
ret // 21
|
||||||
// 22
|
|
||||||
.end // 23
|
.end // 23
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -372,22 +372,22 @@ Here is a more elaborate case study. Given this:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
#include <stdio.h> /* 1 */
|
#include <stdio.h> /* 1 */
|
||||||
/* 2 */
|
|
||||||
struct Person /* 3 */
|
struct Person /* 3 */
|
||||||
{ /* 4 */
|
{ /* 4 */
|
||||||
char * fname; /* 5 */
|
char * fname; /* 5 */
|
||||||
char * lname; /* 6 */
|
char * lname; /* 6 */
|
||||||
int age; /* 7 */
|
int age; /* 7 */
|
||||||
}; /* 8 */
|
}; /* 8 */
|
||||||
/* 9 */
|
|
||||||
extern int rand(); /* 10 */
|
extern int rand(); /* 10 */
|
||||||
extern struct Person * FindOldestPerson(struct Person *, int); /* 11 */
|
extern struct Person * FindOldestPerson(struct Person *, int); /* 11 */
|
||||||
/* 12 */
|
|
||||||
struct Person * OriginalFindOldestPerson(struct Person * people, int length) /* 13 */
|
struct Person * OriginalFindOldestPerson(struct Person * people, int length) /* 13 */
|
||||||
{ /* 14 */
|
{ /* 14 */
|
||||||
int oldest_age = 0; /* 15 */
|
int oldest_age = 0; /* 15 */
|
||||||
struct Person * oldest_ptr = NULL; /* 16 */
|
struct Person * oldest_ptr = NULL; /* 16 */
|
||||||
/* 17 */
|
|
||||||
if (people) /* 18 */
|
if (people) /* 18 */
|
||||||
{ /* 19 */
|
{ /* 19 */
|
||||||
struct Person * end_ptr = people + length; /* 20 */
|
struct Person * end_ptr = people + length; /* 20 */
|
||||||
|
|
@ -403,9 +403,9 @@ struct Person * OriginalFindOldestPerson(struct Person * people, int length) /*
|
||||||
} /* 30 */
|
} /* 30 */
|
||||||
return oldest_ptr; /* 31 */
|
return oldest_ptr; /* 31 */
|
||||||
} /* 32 */
|
} /* 32 */
|
||||||
/* 33 */
|
|
||||||
#define LENGTH 20 /* 34 */
|
#define LENGTH 20 /* 34 */
|
||||||
/* 35 */
|
|
||||||
int main() /* 36 */
|
int main() /* 36 */
|
||||||
{ /* 37 */
|
{ /* 37 */
|
||||||
struct Person array[LENGTH]; /* 38 */
|
struct Person array[LENGTH]; /* 38 */
|
||||||
|
|
@ -440,9 +440,9 @@ serve only to move the location of the `age` member away from offset 0.
|
||||||
same name so that the linker can reconcile the reference to
|
same name so that the linker can reconcile the reference to
|
||||||
`FindOldestPerson`.
|
`FindOldestPerson`.
|
||||||
|
|
||||||
`OriginalFindOldestPerson` takes a pointer to an instance of `struct Person`. Being a pointer,
|
`OriginalFindOldestPerson` takes a pointer to an instance of `struct
|
||||||
this can be used as a way of finding just one instance or, as in our case, an array of these
|
Person`. Being a pointer, this can be used as a way of finding just one
|
||||||
`structs`.
|
instance or, as in our case, an array of these `structs`.
|
||||||
|
|
||||||
The function finds the largest value in the `age` member using the
|
The function finds the largest value in the `age` member using the
|
||||||
expected algorithm. It initializes an `oldest_age` found so far with 0
|
expected algorithm. It initializes an `oldest_age` found so far with 0
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue