Chapter 17

Additional Programming Examples

This chapter presents programming examples from topics presented in previous chapters in the book. The programming designs are accomplished using assembly language only and also using assembly language modules embedded in C programs. Each example shows the outputs obtained from executing the program. There are numerous examples designed in the text and also numerous programs to be designed in the problem set at the end of the chapter.

17.1 Programming Examples

The various topics that will be covered in the examples include: logic instructions, bit test instructions, compare instructions, unconditional and conditional jump instructions, unconditional and conditional loop instructions, fixed-point instructions, floating-point instructions, string instructions, and arrays. The problem set includes the above instructions plus additional select instructions.

Example 17.1 This example uses the following instructions: the bit test instructions bit test and set (BTS), bit test and reset (BTR), and bit test and complement (BTC). These instructions are used in conjunction with the load status flags into AH register (LAHF) instruction and the move (MOV) instruction. The program is designed using an assembly language module embedded in a C program. Figure 17.1(a) shows the program. Figure 17.1(b) shows the outputs.

Figure 17.1

Figure showing program illustrating the use of bit test instructions: (a) the program and (b) the outputs.

Figure showing program illustrating the use of bit test instructions: (a) the program and (b) the outputs.

Figure showing program illustrating the use of bit test instructions: (a) the program and (b) the outputs.

Program illustrating the use of bit test instructions: (a) the program and (b) the outputs.

Example 17.2 This example calculates the sum of cubes for numbers in the following range: 1 to n, where n is ≤ 22. The range number is entered from the keyboard. The sum of cubes can be obtained from any of the expressions shown in Equation 17.1. The three expressions will be used in Examples 17.2, Example 17.3, and Example 17.4, respectively. This example uses the first expression listed using an assembly language module embedded in a C program, as shown in Figure 17.2.

13+23+33+43+...+n3[n(n+1)/2]2(1+2+3+4+...+n)2(17.1)

Figure 17.2

Figure showing program to calculate the sum of cubes in the range from 1 to ≤ 22: (a) the program and (b) the outputs.

Figure showing program to calculate the sum of cubes in the range from 1 to ≤ 22: (a) the program and (b) the outputs.

Figure showing program to calculate the sum of cubes in the range from 1 to ≤ 22: (a) the program and (b) the outputs.

Program to calculate the sum of cubes in the range from 1 to ≤ 22: (a) the program and (b) the outputs.

Example 17.3 This example calculates the sum of cubes for numbers in the following range: 1 to n, where n is ≤ 22. The range number is entered from the keyboard. The sum of cubes for this example is obtained from second expression listed in Equation 17.1 and shown below. This example uses an assembly language module embedded in a C program, as shown in Figure 17.3.

[n(n+1)/2]2

Figure 17.3

Figure showing program to calculate the sum of cubes in the range from 1 to ≤ 22 using the expression shown above: (a) the program and (b) the outputs.

Figure showing program to calculate the sum of cubes in the range from 1 to ≤ 22 using the expression shown above: (a) the program and (b) the outputs.

Program to calculate the sum of cubes in the range from 1 to ≤ 22 using the expression shown above: (a) the program and (b) the outputs.

Example 17.4 This example calculates the sum of cubes for numbers in the following range: 1 to n, where n is ≤ 22. The range number is entered from the keyboard. The sum of cubes for this example is obtained from third expression listed in Equation 17.1 and shown below. This example uses an assembly language module embedded in a C program, as shown in Figure 17.4.

(1+2+3+4+...+n)2

Figure 17.4

Figure showing program to calculate the sum of cubes in the range from 1 to ≤ 22 using the expression shown above: (a) the program and (b) the outputs.

Figure showing program to calculate the sum of cubes in the range from 1 to ≤ 22 using the expression shown above: (a) the program and (b) the outputs.

Figure showing program to calculate the sum of cubes in the range from 1 to ≤ 22 using the expression shown above: (a) the program and (b) the outputs.

Program to calculate the sum of cubes in the range from 1 to ≤ 22 using the expression shown above: (a) the program and (b) the outputs.

Example 17.5 This example replaces all spaces with hyphens in a character string that is entered from the keyboard. The character string is stored in the operand field (OPFLD) of the parameter list (PARLST). The parameter list is reproduced in Figure 17.5 for convenience and illustrates an example for this program. The program in this example uses an assembly language program — not embedded in a C program — as shown in Figure 17.6. This program does not use the MOVSB string instruction — the same program using the MOVSB instruction is left as a problem at the end of the chapter. The comments at the end of each line of code in Figure 17.6 explain the operation of that individual line.

Figure 17.5

Figure showing parameter list one-dimensional array in which the keyboard input data are stored.

Parameter list one-dimensional array in which the keyboard input data are stored.

Figure 17.6

Figure showing program to replace spaces with hyphens in a character string that is entered from the keyboard: (a) the program and (b) the outputs.

Figure showing program to replace spaces with hyphens in a character string that is entered from the keyboard: (a) the program and (b) the outputs.

Figure showing program to replace spaces with hyphens in a character string that is entered from the keyboard: (a) the program and (b) the outputs.

Program to replace spaces with hyphens in a character string that is entered from the keyboard: (a) the program and (b) the outputs.

Example 17.6 This example counts the number of times that the integer 6 occurs in an array of eight integers. The program in this example uses an assembly language program — not embedded in a C program — as shown in Figure 17.7. The array is defined in the data segment (.DATA) and is labelled LIST. The comments at the end of each line of code in Figure 17.7 explain the operation of that individual line. A similar program that uses data entered from the keyboard is left as a problem at the end of the chapter.

Figure 17.7

Figure showing program to count the number of times that the integer 6 occurs in an array of eight integers: (a) the program and (b) the outputs.

Figure showing program to count the number of times that the integer 6 occurs in an array of eight integers: (a) the program and (b) the outputs.

Program to count the number of times that the integer 6 occurs in an array of eight integers: (a) the program and (b) the outputs.

Example 17.7 This example determines the largest integer in an array of eight integers. The program in this example uses an assembly language program — not embedded in a C program — as shown in Figure 17.8. The array is defined in the data segment (.DATA) and is labelled LIST. The comments at the end of each line of code in Figure 17.8 explain the operation of that individual line. A similar program that uses data entered from the keyboard is left as a problem at the end of the chapter.

Figure 17.8

Figure showing program to determine the largest integer in an array of eight integers: (a) the program and (b) the outputs.

Program to determine the largest integer in an array of eight integers: (a) the program and (b) the outputs.

Example 17.8 This example calculates the factorial of single-digit integers in the range of 1 through 8 that are entered from the keyboard. The program in this example is designed using an assembly language module embedded in a C program, as shown in Figure 17.9. A similar program using assembly language only — that uses data entered from the keyboard — is left as a problem at the end of the chapter.

Figure 17.9

Figure showing program to calculate the factorial of single-digit integers that are entered from the keyboard: (a) the program and (b) the outputs.

Figure showing program to calculate the factorial of single-digit integers that are entered from the keyboard: (a) the program and (b) the outputs.

Figure showing program to calculate the factorial of single-digit integers that are entered from the keyboard: (a) the program and (b) the outputs.

Program to calculate the factorial of single-digit integers that are entered from the keyboard: (a) the program and (b) the outputs.

Example 17.9 This example calculates the value of single-digit integer bases in the range of 1 ≤ y ≤ 6 raised to the power of single-digit integer exponents in the range of 0 ≤ × ≤ 6 — bases and exponents are entered from the keyboard. The program in this example is designed using an assembly language module embedded in a C program, as shown in Figure 17.10.

Figure 17.10

Figure showing program to calculate single-digit bases raised to the power of single-digit exponents: (a) the program and (b) the outputs.

Figure showing program to calculate single-digit bases raised to the power of single-digit exponents: (a) the program and (b) the outputs.

Figure showing program to calculate single-digit bases raised to the power of single-digit exponents: (a) the program and (b) the outputs.

Figure showing program to calculate single-digit bases raised to the power of single-digit exponents: (a) the program and (b) the outputs.

Figure showing program to calculate single-digit bases raised to the power of single-digit exponents: (a) the program and (b) the outputs.

Program to calculate single-digit bases raised to the power of single-digit exponents: (a) the program and (b) the outputs.

Example 17.10 This example calculates the value of the following expression using integers for fixed-point arithmetic:

[(A×B)+C+500]/A

The program in this example is designed using an assembly language module embedded in a C program. Both the quotient and remainder will be obtained after the division operation is executed.

Recall that the DIV instruction divides the unsigned dividend in an implied general-purpose register by the unsigned divisor source operand in a register or memory location. The results are stored in the implied registers. The syntax for the DIV instruction is shown below.

DIV register/memory

The locations of the DIV operands are as follows: the dividend registers are AX, DX:AX, EDX:EAX, and RDX:RAX and the size of the corresponding divisors are 8 bits, 16 bits, 32 bits, and 64 bits, respectively; the resulting quotients are stored in registers AL, AX, EAX, and RAX, respectively; the resulting remainders are stored in registers AH, DX, EDX, and RDX, respectively. Results that are not integers are truncated to integers. Figure 17.11 illustrates pictorially the division operation.

Figure 17.11

Figure showing pictorial illustration of the dividend, divisor, quotient, and remainder.

Pictorial illustration of the dividend, divisor, quotient, and remainder.

An example is shown below using the following values for variables A, B, and C: A = 200, B = 200, and C = 1000, yielding a quotient of 207 and a remainder of 100.

[(A×B)+C+500]/A[(200×200)+1000+500]/200=(40000+1000+500)/200=41500/200=207.5

A second example is shown below using the following values for variables A, B, and C: A = 790, B = 650, and C = 1500, yielding a quotient of 652 and a remainder of 420.

[(A×B)+C+500]/A[(790×650)+1500+500]/790=(40000+1000+500)/790=515500/790=652.531645569

Figure 17.12 illustrates a program to calculate the given expression using the fixed-point arithmetic instructions of MUL, ADD, and DIV.

Figure 17.12

Figure showing program to calculate the value of [(A × B) + C + 500] / A: (a) the program and (b) the outputs.

Figure showing program to calculate the value of [(A × B) + C + 500] / A: (a) the program and (b) the outputs.

Program to calculate the value of [(A × B) + C + 500] / A: (a) the program and (b) the outputs.

Example 17.11 This example calculates the value of the following expression using integers for fixed-point arithmetic:

[(A×B)+C+500]/A

This example is similar to Example 17.10, except that doublewords are used and only the quotient is obtained after the division operation is executed; therefore, all fractions are truncated. The program in this example is designed using an assembly language module embedded in a C program and is shown in Figure 17.13.

Figure 17.13

Figure showing program to calculate the value of [(A × B) + C + 500] / A using doubleword operands to obtain the quotient only: (a) the program and (b) the outputs.

Figure showing program to calculate the value of [(A × B) + C + 500] / A using doubleword operands to obtain the quotient only: (a) the program and (b) the outputs.

Program to calculate the value of [(A × B) + C + 500] / A using doubleword operands to obtain the quotient only: (a) the program and (b) the outputs.

Example 17.12 This example calculates the value of the following expression using variables for floating-point arithmetic:

[(A×B)+C+constant]/A

This example is similar to previous examples, except that floating-point operands are used and a floating-point quotient is obtained after the division operation is executed; therefore, all fractions are retained and are not truncated. The program in this example is designed using an assembly language module embedded in a C program.

Recall that floating-point numbers consist of the following three fields: a sign bit, s; an exponent, e; and a fraction, f. These parts represent a number that is obtained by multiplying the fraction, f , by a radix, r, raised to the power of the exponent, e, as shown in Equation 17.2 for the number A, where f and e are signed fixed-point numbers, and r is the radix (or base).

A=f×re(17.2)

Figure 17.14 shows the format for 32-bit single-precision floating-point numbers. The single-precision format consists of a sign bit that indicates the sign of the number, an 8-bit signed exponent, and a 23-bit unsigned fraction.

Figure 17.14

Figure showing format for single-precision floating-point numbers.

Format for single-precision floating-point numbers.

Figure 17.15 shows the eight data registers — called the register stack — used in the floating-point unit. The stack top, ST(0), is register R0 and, like a normal stack, builds towards lower-numbered registers. The register immediately below the stack top is referred to as ST(1); the register immediately below ST(1) is referred to as ST(2), etc.

Figure 17.15

Figure showing double extended-precision register stack for the floating-point unit.

Double extended-precision register stack for the floating-point unit.

When the stack is full; that is, the registers at ST(0) through the register at ST(7) contain valid data, a stack wraparound occurs if an attempt is made to store additional data on the stack. This results in a stack overflow because the unsaved data is overwritten. The stack registers are specified by three bits — 000 through 111 — to reference ST(0) through ST(7), respectively. Therefore, ST(i) references the ith register from the current stack top. Chapter 11 can be reviewed for more details on floatingpoint operations.

Figure 17.16 shows the assembly language module embedded in a C program to perform the calculations for the expression shown in Figure 17.16. Figure 17.17 shows the contents of the register stack during execution of the program for variables X, Y, Z, and a constant that are equal to 4.0, 5.0, 6.0, and 7.0, respectively.

Figure 17.16

Figure showing program to perform calculations on [(X × Y) + Z + constant] / X: (a) the program and (b) the outputs.

Figure showing program to perform calculations on [(X × Y) + Z + constant] / X: (a) the program and (b) the outputs.

Figure showing program to perform calculations on [(X × Y) + Z + constant] / X: (a) the program and (b) the outputs.

Program to perform calculations on [(X × Y) + Z + constant] / X: (a) the program and (b) the outputs.

Figure 17.17

Figure showing register stack for Example 17.12.

Register stack for Example 17.12.

Assume that instructions FLD flp_x and FLD flp_y have been executed.

Example 17.13 This example calculates the value of the following expression using x as a variable for floating-point arithmetic:

x35x25x+x

Figure 17.18 shows the assembly language module embedded in a C program to perform the calculations for the given expression. Figure 17.19 shows the contents of the register stack during execution of the program for the variable x that is equal to 2.0.

Figure 17.18

Figure showing program to calculate the given expression for different values of x: (a) the program and (b) the outputs.

Figure showing program to calculate the given expression for different values of x: (a) the program and (b) the outputs.

Figure showing program to calculate the given expression for different values of x: (a) the program and (b) the outputs.

Program to calculate the given expression for different values of x: (a) the program and (b) the outputs.

Figure 17.19

Figure showing register stack for Example 17.13.

Figure showing register stack for Example 17.13.

Register stack for Example 17.13.

Example 17.14 This example calculates the value of the following trigonometric expressions using floating-point arithmetic:

[(sine  30°)(cosine  45°)][(sine  30°)  ×(cosine  45°)][(sine  30°)  ×(cosine  45°)]

Figure 17.20 shows the assembly language module embedded in a C program to perform the calculations for the given trigonometric expressions. Note that the degrees must be given in radians. Figure 17.21 shows the contents of the register stack during execution of the program for the [(sine 30°) – (cosine 45°)] operation.

Figure 17.20

Figure showing program to calculate the values of the given trigonometric expressions: (a) the program and (b) the outputs.

Figure showing program to calculate the values of the given trigonometric expressions: (a) the program and (b) the outputs.

Program to calculate the values of the given trigonometric expressions: (a) the program and (b) the outputs.

Figure 17.21

Figure showing register stack for Example 17.14.

Register stack for Example 17.14.

Example 17.15 This example calculates the value of the following trigonometric expression using floating-point arithmetic:

(5×cos30°)(2×sine 60°)

Figure 17.22 shows the assembly language module embedded in a C program to perform the calculation for the given trigonometric expression. Note that the degrees must be given in radians.

Figure 17.22

Figure showing program to calculate the value of the given trigonometric expression: (a) the program and (b) the outputs.

Figure showing program to calculate the value of the given trigonometric expression: (a) the program and (b) the outputs.

Program to calculate the value of the given trigonometric expression: (a) the program and (b) the outputs.

Example 17.16 This example uses the move data from string to string for bytes (MOVSB) instruction to overwrite data in a given string. The repeat prefix (REP) is also used in the program to copy four bytes from the given string to a different location in the same string. Figure 17.23 shows the assembly language program — not embedded in a C program — to perform the operation.

Figure 17.23

Figure showing program to overwrite string locations: (a) the program and (b) the output.

Figure showing program to overwrite string locations: (a) the program and (b) the output.

Program to overwrite string locations: (a) the program and (b) the output.

Example 17.17 This example is similar to Example 17.16; however, the string is entered from the keyboard. The move data from string to string for bytes (MOVSB) instruction is also used together with the repeat prefix (REP) to replace the last four characters of a 10-character string with the first four characters. Figure 17.24 shows the assembly language program — not embedded in a C program — to perform the operation.

Figure 17.24

Figure showing program to exchange the last four characters of a 10-character string with the first four characters: (a) the program and (b) the outputs.

Figure showing program to exchange the last four characters of a 10-character string with the first four characters: (a) the program and (b) the outputs.

Program to exchange the last four characters of a 10-character string with the first four characters: (a) the program and (b) the outputs.

Example 17.18 This example converts an integer (1 — 26) that is entered from the keyboard to the corresponding uppercase alphabetic character. An array is used to define the uppercase alphabetic characters. Figure 17.25 illustrates the program written in the C programming language.

Figure 17.25

Figure showing program to convert an integer to the corresponding uppercase alphabetic character: (a) the program and (b) the outputs.

Figure showing program to convert an integer to the corresponding uppercase alphabetic character: (a) the program and (b) the outputs.

Program to convert an integer to the corresponding uppercase alphabetic character: (a) the program and (b) the outputs.

Example 17.19 This example utilizes an array to translate a 2-digit number that is entered from the keyboard to the correct spelling of the corresponding number. The 2-digit numbers are in the following range: 01 — 12. Figure 17.26 shows the assembly language program — not embedded in a C program — to perform the translation operation. Since the length of the longest number is six characters, each table entry in the array must contain six locations.

Figure 17.26

Figure showing program to translate a 2-digit number to the correct spelling of the number: (a) the program and (b) the outputs.

Figure showing program to translate a 2-digit number to the correct spelling of the number: (a) the program and (b) the outputs.

Figure showing program to translate a 2-digit number to the correct spelling of the number: (a) the program and (b) the outputs.

Program to translate a 2-digit number to the correct spelling of the number: (a) the program and (b) the outputs.

17.2 Problems

  1. 17.1 Write a program in assembly language — not embedded in a C program — that determines the largest number from eight single-digit numbers that are entered from the keyboard, then display the number.

  2. 17.2 Write a program in assembly language — not embedded in a C program — that counts the number of times that a number occurs in an array of eight numbers. The first number entered from the keyboard is the number that is being compared to the remaining eight numbers. Display the result.

  3. 17.3 Write a program in assembly language — not embedded in a C program — that translates an ASCII character (0 through 9) that is entered from the keyboard to the letters a through j, respectively.

  4. 17.4 Write an assembly language module embedded in a C program that uses the exclusive-OR function to operate on a string of hexadecimal characters that are entered from the keyboard. Perform the operation on the following character positions in the specified sequence, then display the results:

    Position   1position  3position  3Position  2position  4position  3Position  3position  5position  3Position  4position  6position  3

  5. 17.5 Write a program in assembly language — not embedded in a C program — that calculates the factorial of numbers entered from the keyboard in the range of 1 through 6. Display the results.

  6. 17.6 Write an assembly language module embedded in a C program to obtain the sum of cubes for the numbers 1 through 5. Display the resulting sum of cubes.

  7. 17.7 Write an assembly language module embedded in a C program to calculate 2 to power of n (0 – 8), where the variable n is entered from the keyboard.

  8. 17.8 Write an assembly language module embedded in a C program to evaluate the following expression using fixed-point integers for bytes, words, and double-words and obtain the quotients only:

    [(X×Y)+Z+constant]

  9. 17.9 Write an assembly language module embedded in a C program to evaluate the expression shown below for Y using a floating-point number for the variable X. The range for X is –3.0 ≤ X ≤ +3.0. Enter several numbers for X and display the corresponding results.

    Y=X310X2+20X+30

  10. 17.10 Write an assembly language module embedded in a C program to determine the result of the equation shown below, where L and C are floating-point numbers. The variables L and C are entered from the keyboard. Display the results.

    Result=1/[2×π×(L×C)1/2]

  11. 17.11 Given the program segment shown below, obtain the results for the following floating-point numbers: 2.0, 10.0, and 15.0.

          . . .
    FLD flp_num
    FMUL ST(0), ST(0)
    FLDPI
    FMUL ST(0), ST(1)
    FST rslt
          . . .
  12. 17.12 Given the program segment shown below, obtain the results for the following floating-point numbers:

    flp_num1 = 125.0,  flp_num2 = 245.0

    flp_num1 = 650.0,  flp_num2 = 375.0

    flp_num1 = 755.125, flp_num2 = 575.150
          . . .
    FLD  flp_num1
    FSQRT
    FLD  flp_num2
    FSQRT
    FSUBP ST(1), ST(0)
    FST  rslt
          . . .
  13. 17.13 Write an assembly language module embedded in a C program to determine the result of the following equation:

    (sine  90°sine  30°)/(cosine  180°+cosine  60°)

  14. 17.14 Write an assembly language module embedded in a C program to calculate the tangent of the following angles using the sine and cosine of the angles:

    30°,45°, and 60°

  15. 17.15 Write an assembly language program — not embedded in a C program — that replaces all spaces with hyphens in a character string entered from the keyboard.