Chapter 10

  1. I’ll let you do this on your own.
  2. Return an integer
    • test_f.c
      // Test f() function.
      
      #include <stdio.h>
      #include "f.h"
      
      int main(void)
      {
          int return_value;
          return_value = f();
          printf("f returned %i.\n", return_value);
      
          return 0;
      }
      
    • f.h
      // Return 0.
      
      #ifndef F_H
      #define F_H
      int f(void);
      #endif
      
    • f.s
      // Minimum components of a C function, in assembly language
      // returns 0
      
              .arch   armv8-a
              .text
              .align  2
              .global f
              .type   f, %function
      f:
              mov     w0, wzr         // return 0;
              ret
      
  3. Return three integers.
    • test_ints.c
      // Test three functions that return ints.
      
      #include <stdio.h>
      #include "twelve.h"
      #include "thirty_four.h"
      #include "fifty_six.h"
      
      int main(void)
      {
          int return1, return2, return3;
      
          return1 = twelve();
          return2 = thirty_four();
          return3 = fifty_six();
          printf("The returned ints are: %i, %i, and %i.\n",
                return1, return2, return3);
      
          return 0;
      }
      
    • twelve.h
      // Return 12.
      
      #ifndef TWELVE_H
      #define TWELVE_H
      int twelve(void);
      #endif
      
    • twelve.s
      // Return 12.
      
              .arch   armv8-a
              .text
              .align  2
              .global twelve
              .type   twelve, %function
      twelve:
              mov     w0, 12
              ret
      
    • thirty_four.h
      // Return 34.
      
      #ifndef THIRTY_FOUR_H
      #define THIRTY_FOUR_H
      int thirty_four(void);
      #endif
      
    • thirty_four.s
      // Return 34.
      
              .arch   armv8-a
              .text
              .align  2
              .global thirty_four
              .type   thirty_four, %function
      thirty_four:
              mov     w0, 34
              ret
      
    • fifty_six.h
      // Return 56.
      
      #ifndef FIFTY_SIX_H
      #define FIFTY_SIX_H
      int fifty_six(void);
      #endif
      
    • fifty_six.s
      // Return 56.
      
              .arch   armv8-a
              .text
              .align  2
              .global fifty_six
              .type   fifty_six, %function
      fifty_six:
              mov     w0, 56
              ret
      
  4. Return three characters.
    • test_chars.c
      // Test three functions that return chars.
      
      #include <stdio.h>
      #include "exclaim.h"
      #include "upper_oh.h"
      #include "tilde.h"
      
      int main(void)
      {
          unsigned char return1, return2, return3;
      
          return1 = exclaim();
          return2 = upper_oh();
          return3 = tilde();
          printf("The returned chars are: %c, %c, and %c.\n",
                return1, return2, return3);
      
          return 0;
      }
      
    • exclaim.h
      // Return '!'.
      
      #ifndef EXCLAIM_H
      #define EXCLAIM_H
      char exclaim(void);
      #endif
      
    • exclaim.s
      // Return '!'.
      
              .arch   armv8-a
              .text
              .align  2
              .global exclaim
              .type   exclaim, %function
      exclaim:
              mov     w0, '!'
              ret
      
    • upper_oh.h
      // Return 'O'.
      
      #ifndef UPPER_OH_H
      #define UPPER_OH_H
      unsigned char upper_oh(void);
      #endif
      
    • upper_oh.s
      // Return 'O'.
      
              .arch   armv8-a
              .text
              .align  2
              .global upper_oh
              .type   upper_oh, %function
      upper_oh:
              mov     w0, 'O'
              ret
      
    • tilde.h
      // Return '~'.
      
      #ifndef TILDE_H
      #define TILDE_H
      unsigned char tilde(void);
      #endif
      
    • tilde.s
      // Return '~'.
      
              .arch   armv8-a
              .text
              .align  2
              .global tilde
              .type   tilde, %function
      tilde:
              mov     w0, '~'
              ret