## Exercises12.2Programming Exercises

###### 1.

Assume that you do not know how many numerals there are, only that the first one is ‘$$0$$’ and the last one is ‘$$9$$’. Write a program in assembly language that displays all the numerals, $$0,\ldots,9$$ on the screen, one character at a time. Do not allocate a separate character for each numeral.

Hint

You can load the character ‘$$0$$’ into a register with the mov r4, #'0 instruction. And don't forget that the write function requires that you pass it the address of the byte to write.

Solution
@ numerals.s
@ Displays the numerals, 0 - 9
@ 2017-09-29: Bob Plantz

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

@ Useful source code constants
.equ    STDOUT,1
.equ    numeral,-20
.equ    local,8

@ Constant program data
.section .rodata
.align  2
@ The program
.text
.align  2
.global main
.type   main, %function
main:
sub     sp, sp, 16      @ space for saving regs
@ (keeping 8-byte sp align)
str     r4, [sp, 4]     @ save r4
str     fp, [sp, 8]     @      fp
str     lr, [sp, 12]    @      lr
add     fp, sp, 12      @ set our frame pointer
sub     sp, sp, local   @ allocate memory for local var

mov     r4, '0          @ numeral 0
loop:
strb    r4, [fp, numeral]  @ char must be
@ in memory for write
mov     r0, STDOUT      @ write to screen
mov     r2, 1           @ one byte
bl      write

add     r4, r4, 1       @ next numeral
cmp     r4, '9          @ all numerals?
ble     loop            @ no, keep going

mov     r0, 0           @ yes, return 0;
add     sp, sp, local   @ deallocate local var
ldr     r4, [sp, 4]     @ restore r4
ldr     fp, [sp, 8]     @         fp
ldr     lr, [sp, 12]    @         lr
add     sp, sp, 16      @         sp
bx      lr              @ return

###### 2.

Assume that you do not know how many alphabetic characters there are, only that the first one is ‘A’ and the last one is ‘Z’. Write a program in assembly language that displays all the letters, A,…, Z on the screen, one character at a time. Do not allocate a separate character for each numeral.

Hint

You can load the character ‘A’ into a register with the mov r4, #'A instruction. And don't forget that the write function requires that you pass it the address of the byte to write.

Solution
@ letters.s
@ Displays the upper case alphabet, A - Z
@ 2017-09-29: Bob Plantz

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

@ Useful source code constants
.equ    STDOUT,1
.equ    alpha,-20
.equ    local,8

@ Constant program data
.section .rodata
.align  2
@ The program
.text
.align  2
.global main
.type   main, %function
main:
sub     sp, sp, 16      @ space for saving regs
@ (keeping 8-byte sp align)
str     r4, [sp, 4]     @ save r4
str     fp, [sp, 8]     @      fp
str     lr, [sp, 12]    @      lr
add     fp, sp, 12      @ set our frame pointer
sub     sp, sp, local   @ allocate memory for local var

mov     r4, 'A          @ beginning of alphabet
loop:
strb    r4, [fp, alpha] @ char must be
@ in memory for write
mov     r0, STDOUT      @ write to screen
mov     r2, 1           @ one byte
bl      write

add     r4, r4, 1       @ next alpha
cmp     r4, 'Z          @ whole alphabet?
ble     loop            @ no, keep going

mov     r0, 0           @ yes, return 0;
add     sp, sp, local   @ deallocate local var
ldr     r4, [sp, 4]     @ restore r4
ldr     fp, [sp, 8]     @         fp
ldr     lr, [sp, 12]    @         lr
add     sp, sp, 16      @         sp
bx      lr              @ return