Skip to main content
\(\newcommand{\doubler}[1]{2#1} \newcommand{\binary}{\mathtt} \newcommand{\hex}{\mathtt} \newcommand{\octal}{\mathtt} \newcommand{\prog}{\mathtt} \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \)

Section14.4Programming Exercises

1

Show that the main function in Listing 14.3.1 works with the assembly language hexToInt function in Listing 14.3.5

Solution

You will need to copy the assembly language code for hexToInt into a file and then assemble it with:


      as --gstabs -o hexToInt2.o hexToInt2.s

Then use gcc to link the object files with the file containing the main function:


      gcc -g -o hexConvert1 hexToInt2.o writeStr.o readLn.o hexConvert1.c
2

Does the program in in Exercise 14.4.1 work correctly for both lowercase and uppercase alphabetic hex characters? Why or why not?

Hint

Review the numerical order of characters in the ASCII code.

Answer

It works for both cases because all alphabetic characters are numerically higher than the numeral characters in the ASCII code.

3

Write an assembly language version of the main function in Listing 14.3.1. Use the assembly language versions of hexToInt, writeStr, and readLn in your program. You may use printf from the C Standard Library to display your result.

Hint

You do not need to include the stdio.h header file. This is used to tell the compiler what to do with the call to printf, but you will be writing the assembly language code to do this yourself.

Solution
@ hexConvert2.s
@ Prompts user for hex number and converts
@ it to an int.
@ 2017-09-29: Bob Plantz

@ Define my Raspberry Pi
        .cpu    cortex-a53
        .fpu    neon-fp-armv8
        .syntax unified         @ modern syntax

@ Constant for assembler
        .equ    maxChars,9      @ max input chars
        .equ    theString,-16   @ for input string
        .equ    locals,16       @ space for local vars

@ Constant program data
        .section .rodata
        .align  2
prompt:
        .asciz        "Enter up to 32-bit hex number: "
display:
        .asciz        "The integer is: %i\n"

@ The program
        .text
        .align  2
        .global main
        .type   main, %function
main:
        sub     sp, sp, 8       @ space for fp, lr
        str     fp, [sp, 0]     @ save fp
        str     lr, [sp, 4]     @   and lr
        add     fp, sp, 4       @ set our frame pointer
        sub     sp, sp, locals  @ for local vars
        
        ldr     r0, promptAddr  @ prompt user
        bl      writeStr
        
        add     r0, fp, theString  @ place for user input
        mov     r1, maxChars    @ limit input size
        bl      readLn
        
        add     r0, fp, theString  @ user input
        bl      hexToInt        @ convert it

        mov     r1, r0          @ result returned in r0
        ldr     r0, displayAddr @ show user the result
        bl      printf          @ from C Standard Lib.
        
        mov     r0, 0           @ return 0;
        add     sp, sp, locals  @ deallocate local var
        ldr     fp, [sp, 0]     @ restore caller fp
        ldr     lr, [sp, 4]     @       lr
        add     sp, sp, 8       @   and sp
        bx      lr              @ return

promptAddr:
        .word    prompt
displayAddr:
        .word    display
4

Write a program in assembly language that asks the user to enter two hexadecimal numbers, each up to 32 bits, and displays their sum.

Hint

You will need to allocate a second place in memory for holding the second input text string.

Solution
@ addHex.s
@ Prompts user for two hex numbers and adds them
@ 2017-09-29: Bob Plantz

@ Define my Raspberry Pi
        .cpu    cortex-a53
        .fpu    neon-fp-armv8
        .syntax unified         @ modern syntax

@ Constant for assembler
        .equ    maxChars,9      @ max input chars
        .equ    inString1,-24   @ for 1st input string
        .equ    inString2,-36   @ for 2nd input string
        .equ    outString,-52   @ for output string
        .equ    locals,40       @ space for local vars

@ Constant program data
        .section .rodata
        .align  2
prompt:
        .asciz        "Enter up to 32-bit hex number: "
display:
        .asciz        "Their sum is: "

@ The program
        .text
        .align  2
        .global main
        .type   main, %function
main:
        sub     sp, sp, 16      @ space for saving regs
        str     r4, [sp, 0]     @ save r4
        str     r5, [sp, 4]     @      r5
        str     fp, [sp, 8]     @      fp
        str     lr, [sp, 12]    @      lr
        add     fp, sp, 12      @ set our frame pointer
        sub     sp, sp, locals  @ for local vars
        
        ldr     r0, promptAddr  @ prompt user
        bl      writeStr
        
        add     r0, fp, inString1  @ 1st input
        mov     r1, maxChars    @ limit input size
        bl      readLn
        add     r0, fp, inString1  @ user input
        bl      hexToInt        @ convert it
        mov     r4, r0          @ 1st int

        ldr     r0, promptAddr  @ prompt for 2nd
        bl      writeStr

        add     r0, fp, inString2  @ 2nd input
        mov     r1, maxChars    @ limit input size
        bl      readLn
        add     r0, fp, inString2  @ user input
        bl      hexToInt        @ convert it
        mov     r5, r0          @ 2nd int

        add     r1, r4, r5      @ add the two ints
        add     r0, fp, outString  @ place for result
        bl      intToHex        @ convert to hex string
        
        ldr     r0, displayAddr @ show user result
        bl      writeStr
        
        add     r0, fp, outString
        bl      writeStr
        
        bl      newLine         @ looks nicer
        
        mov     r0, 0           @ return 0;
        add     sp, sp, locals  @ deallocate local var
        ldr     r4, [sp, 0]     @ restore r4
        ldr     r5, [sp, 4]     @      r5
        ldr     fp, [sp, 8]     @         fp
        ldr     lr, [sp, 12]    @         lr
        add     sp, sp, 16      @ restore sp
        bx      lr              @ return

promptAddr:
        .word    prompt
displayAddr:
        .word    display