ch03 - Chapter 3 Exercise Solutions E3.1 There are many...

This preview shows pages 1–4. Sign up to view the full content.

Chapter 3 Exercise Solutions E3.1 There are many ways to solve this problem, one of them is as follows: LDX #\$4546 PSHX LDX #\$4344 PSHX LDX #\$4142 PSHX E3.2 The following instructions will load the 10th element of the array into A: LDX #ARRAY LDAA 9,X ; assume the index starts from 0 E3.3 The median will be placed in accumulator A. The program that finds the array with 21 8-bit elements is in the following: N EQU 21 ; use directive equ makes the program applicable * ; to other array size ORG \$D000 vect FCB ...... FCB ...... ORG \$C000 LDX #vect ; place the starting address in X LDAB #N/2 LSLB ; multiply B by 2 CMPB #N BNE nisodd ; array count is odd LDAB #N/2-1 ABX LDAA 0,X ; place the element vect[N/2 - 1] in A INX ; move to the next array element ADDA0,X ; compute vect[N/2 - 1] + vect[N/2] ASRA ; compute the average BRA exit nisodd LDAB #N/2 ABX ; compute the address of vect[N/2] LDAA 0,X ; get the array median exit END E3.4 The program is as follows: N EQU ..... ; N is set to equal to the array count ORG \$D000 min RMB 1 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
max RMB 1 mean RMB 1 key FCB 1 array FCB .... FCB .... ORG \$C000 CLR min ; min is initialized to 0 LDAA #N-1 STAA max ; max is initialized to N-1 loop LDAA min CMPA max ; BHI notfound ; the key is not found LDAB min ; get the low index value ADDB max ; add the high index value LSRB ; compute (min+max)/2 LDX #array ABX ; compute the address of the element to be compare LDAA 0,X ; get the element to be compared CMPA key ; compare to the key BLO gohigh ; check the element with higher index value BHI golow ; check the element with lower index value BRA exit gohigh INCB STAB min ; set min to mean + 1 BRA loop golow DECB STAB max ; set max to mean - 1 BRA loop notfound LDX #\$FFFF ; place \$FFFF in X if no element equals to the key exit NOP END E3.5 (a) the address of the (2,3)th element is \$D000 + 3 * 12 + 2 = \$D026. (b) the address of the (4,9)th element is \$D000 + 9 * 12 + 4 = \$D070. (c) the address of the (10,8)th element is \$D000 + 8 * 12 + 10 = \$D06A. E3.6 (a) the address of the (4, 1)th element is \$D100 + 2*(4 * 9 + 1) = \$D14A (b) the address of the (5, 2)th element is \$D100 + 2*(5 * 9 + 2) = \$D15E (c) the address of the (7, 5)th element is \$D100 + 2*(7 * 9 + 5) = \$D188 E3.7 The element of the product matrix C is computed using the following equation: 2
N EQU 6 ORG \$D000 i RMB 1 ; row index j RMB 1 ; column index k RMB 1 ; running index temp0 RMB 2 MA FCB .... MB FCB .... MC RMB N×N×2 ; reserve 72 bytes for matrix product ORG \$C000 ; starting address of the program CLR i ; initialize i to 0 iloop CLR j ; initialize j to 0 jloop LDAA i ; place row index in A LDAB #N MUL ADDB j ; compute i × n + j ADCA #0 LSLD ; compute 2*(i × n + j) ADDD #MC XGDX ; X points to MC(i,j) CLR 0,X ; initialize MC(i,j) to 0 CLR 1,X ; CLR k ; initialize k to 0 kloop LDAA i LDAB #N MUL ADDB k ; compute i × n + k ADCA #0 ADDD #MA STD temp0 ; save the address of MA(i,k) LDAA k LDAB #N MUL ADDB j ; compute k × n + j

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 18

ch03 - Chapter 3 Exercise Solutions E3.1 There are many...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online