E N D
1. Chapter 4Advanced Assembly Programming
2. Introduction Program = data structures + algorithm
Data structures to be discussed
Stacks: a first-in-last-out data structure
Arrays: a set of elements of the same type
Strings: a sequence of characters terminated by a special character
Stack
3. HCS12 Support for the Stack Data Structure A 16-bit stack pointer (SP)
Instructions and addressing mode
4. Indexable Data Structures Vectors and matrices are indexable data structures.
The first element of a vector is associated with the index 0 in order to facilitate the address calculation.
Assemblers directives db, dc.b, dc.b are used to define arrays of 8-bit elements.
Assemblers directives dw, dc.w, and fdb are used to define arrays of 16-bit elements.
Example 4.2
Write a program to find out if the array vec_x contains a value key.
The array has 16-bit elements and is not sorted.
Solution:
Use the double accumulator to hold the key.
Use the index register X as a pointer to the array.
Use the index register Y to hold the loop count.
Need to compare key with every array element because it is not sorted.
7. Binary Search Step 1
Initialize variables max and min to n -1 and 0, respectively.
Step 2
If max < min, then stop. No element matches the key.
Step 3
Let mean = (max + min)/2
Step 4
If key = arr[mean], then key is found in the array, exit.
Step 5
If key < arr[mean], then set max to mean - 1 and go to step 2.
Step 6
If key > arr[mean], then set min to mean + 1 and go to step 2.
10. Strings A sequence of characters terminated by a NULL (ASCII code 0) or other special character such as EOT (ASCII code 4).
A number in the computer is represented as a binary number.
A number must be converted to ASCII code before output so that it can be understood.
To convert a binary into an ASCII string, we divide the binary number by 10 repeatedly until the quotient is zero. For each remainder, the number $30 is added.
Example 4.4 Write a program to convert the unsigned 8-bit binary number in accumulator A into BCD digits terminated by a NULL character. Each digit is represented in ASCII code.
Solution:
4 bytes are needed to hold the converted BCD digits.
Repeated division by 10 method is used.
13. Example 4.5 Write a program to convert the 16-bit signed integer in D into a string of BCD digits.
Solution
A signed 16-bit integer is in the range of -32768 to +32767.
A NULL character is needed to terminate the converted BCD string.
A minus character is needed for a negative number.
Up to 7 bytes are needed to hold the converted result.
20. Character and Word Counting A string is terminated by the NULL character.
A new word is identified by skipping over the white space characters.
When a new word is identified, it must be scanned through before the next word can be identified.
24. Word Matching
30. String Insertion (1 of 2) The pointers to the string and the substring to be inserted are given.
The insertion point is given.
The procedure is given in Figure 4.6.
34. Subroutines A sequence of instructions that can be called from various places in the program
Allows the same operation to be performed with different parameters
Simplifies the design of a complex program by using the divide-and-conquer approach
Instructions related subroutine calls
35. Program Structure
36. Subroutine Processing
37. Issues in Subroutine Calls Parameter passing
Use registers
Use the stack
Use global memory
Returning results
Use registers
Use the stack (caller created a hole in which the result will be placed)
Use global memory Local variables allocation
Allocated by the callee
Use the following instruction is the most efficient way for local variable allocation
leas -n,sp ;
allocate n bytes in the stack for local variables
Local variables deallocation
Performed by the subroutine
Use the following instruction is the most efficient way
leas n,sp ;
deallocate n bytes from the stack
38. Stack Frame (1 of 2) The region in the stack that holds incoming parameters, the subroutine return address, local variables, and saved registers is referred to as stack frame.
The stack frame is also called activation record.
40. Examples of Subroutines Algorithm for finding the greatest common divisor of Integers m and n
Step 1
If m = n then
gcd ¬ m;
return;
Step 2
If n < m then swap m and n.
Step 3
gcd ¬ 1.
If m = 1 or n = 1 then return.
Step 4
p = n % m;
Step 5
if (p == 0) then m is the gcd.
else
n ¬ m;
m ¬ p;
goto Step 4.
44. Multi-Byte Division (1 of 2) The HCS12 provides instructions for performing 16-bit by 16-bit or 32-bit by 16-bit division
The HCS12 does not have division instructions for larger numbers.
Larger number divisions must be implemented by repeated subtraction method.
The conceptual hardware for performing high precision division is shown in Figure 4.12.
52. Sorting is useful for improving the searching speed when an array or a file needs to be searched many times.
Bubble sort is a simple, but inefficient sorting method.
Example 2.13
Write a subroutine to implement the bubble sort algorithm and a sequence of instructions for testing the subroutine.
Pass the base address of the array and the array count in the stack.
Four bytes are needed for local variables. Bubble Sort
57. Finding the Square Root (1 of 2) One of the methods for finding the square root of an integer is based on the following equation:
Equation 4.1 can be transformed into
The algorithm for finding the square root of an integer based on equation 4.2 is illustrated in the flowchart shown in Figure 4.16.
59. Example 4.14 Write a subroutine to implement the square root algorithm. This subroutine should be able to find the square root of a 32-bit unsigned integer. The parameter is pushed onto the stack and the square root is returned in accumulator D.
Solution: The stack frame is shown in Figure 4.17. The subroutine and the instruction sequence for testing the subroutine is shown in the following pages.
63. Using the D-Bug12 Functionsto Perform I/O Operations (1 of 3)
64. All functions listed in Table 4.2 are written in C language.
The first parameter to the function is passed in accumulator D. The remaining parameters are pushed onto the stack in the reverse order they are listed in the function declaration.
Parameters of type char will occupy the lower order byte of a word pushed onto the stack.
Parameters pushed onto the stack before the function is called remain on the stack when the function returns. It is the responsibility of the caller to remove passed parameters from the stack.
All 8- and 16-bit values are returned in accumulator D. A returned value of type char is returned in accumulator B. Using the D-Bug12 Functionsto Perform I/O Operations (2 of 3)
67. The Meaning of Optional Characters
68. Formatting Characters Supported by the printf() function:
70. int far GetCmdLine(char *CmdLineStr, int CmdLineLen)
Pointer address: $EE8A
Incoming parameters: a pointer to the buffer where the input string is to be stored and the maximum number of characters that will be accepted by this function.
This function is used to obtain a line of input from the user.
The reception of an ASCII carriage return ($0D) terminates the reception of characters from the user.
The caller of this function normally would output a message so that the user knows to enter a message. The example in the next page illustrates this interaction.
72. Example 4.15 Write a program that invokes appropriate functions to find the prime number between 1000 and 2000. Output eight prime numbers in one line. To do this, you will need to
Write a subroutine to test if an integer is a prime.
Invoke the printf() function to output the prime number.
Write a loop to test all the integers between 1000 and 2000.
Solution: The logic structure of the program is
Step 1
Output the message “The prime numbers between 1000 and 2000 are as follows:”.
Step 2
For every number between 100 and 1000
call the test_prime() function to see if it is a prime.
output the number (call printf()) if it is a prime.
if there are already eight prime numbers in the current line, then also output a carriage return.
73. Step 1
Let num, i, and isprime represent the number to be tested, the loop index, and the flag to indicate if num is prime.
Step 2
isprime ¬ 0;
Step 3
For i = 2 to num/2 do if num % i = 0 then return; isprime ¬ 1; return;
The Stack frame of the function test_prime (): The Algorithm of the test_prime()
79. Program Execution Result
80. Tips for Program Debugging Involving Subroutine Calls What to do when the program gets stuck?
Step 1
Find out which subroutine gets stuck by setting a breakpoint immediately after the jsr or bsr instruction.
Step 2
Find out why the subroutine gets stuck.
Forgetting to restore registers pushed onto the stack before return.
Forgetting to deallocate local variables before return.
There are some infinite loops in the subroutine.
Calling other subroutines that do not return.
81. General Debugging Strategy Make sure all leaf subroutines work correctly by using the methods described in Section 2.9.
Debug intermediate subroutines. Make sure no intermediate subroutines get stuck.
Debug the top level program.