Using CodeWarrior, develop the following assembly program for the HCS12 CPU. The
ID: 669232 • Letter: U
Question
Using CodeWarrior, develop the following assembly program for the HCS12 CPU. The numbers (both input and output) are to be stored in RAM and they should be declared within a data section using the assembler directive ds.w.
1) Create a program that converts a 2-digit unsigned BCD (binary coded decimal) number to an 8-bit unsigned binary. The BCD number is smaller than 100ten. Each digit of the BCD number is stored as an ASCII character that occupies one byte. For example, a value of 18ten in BCD is stored as $31, followed by a $38.
2) Modify the previous program so that the BCD number is no longer unsigned. Instead it is a 4-digit number with the first digit as either “+” ($2B in ASCII) or “-” ($2D in ASCII). The 8-bit binary number should be in 2’s complement format. For example, + 108 is to be converted to 0110 1100 while -108 is converted to 1001 0100. If the value is within the range of an 8-bit signed number, i.e., between and including 127 and -128, turn off LED1; otherwise, turn on LED1 without any conversion.
3) Modify the program to convert an 8-bit signed number in 2’s complement format to a 4-digit BCD number in ASCII. (The output of Experiment 3 is the same as the input to Experiment 2 and vise-versa.) There is no need for overflow detection.
Explanation / Answer
Answer 1) Program :
minus equ $2D ; ASCII code of minus sign
org $1000
in_buf fcc "9889" ; input ASCII to be converted
dc.b 0 ; null character
out_buf ds.b 2 ; holds the converted binary value
buf2 ds.b 1 ; holds a zero
buf1 ds.b 1 ; holds the current digit value
sign ds.b 1 ; holds the sign of the number
error ds.b 1 ; indicates the occurrence of illegal character
org $1500
clr sign
clr error
clr out_buf
clr out_buf+1
clr buf2
ldx #in_buf
ldaa 0,x
cmpa #minus ; is the first character a minus sign?
bne continue ; branch if not minus
inc sign ; set the sign to 1
inx ; move the pointer
continue ldaa 1,x+ ; is the current character a NULL character?
lbeq done ; yes, we reach the end of the string
cmpa #$30 ; is the character not between 0 to 9?
lblo in_error ; "
cmpa #$39 ; "
lbhi in_error ; "
suba #$30 ; convert to the BCD digit value
staa buf1 ; save the digit temporarily
ldd out_buf
ldy #10
emul ; perform 16-bit by 16-bit multiplication
addd buf2 ; add the current digit value
std out_buf ; Y holds 0 and should be ignored
bra continue
in_error ldaa #1
staa error
done ldaa sign ; check to see if the original number is negative
beq positive
ldaa out_buf ; if negative, compute its two’s complement
ldab out_buf+1 ; “
coma ; “
comb ; “
addd #1 ; “
std out_buf
positive swi
end
Answer 2) Program :
test_dat equ -2478
org $1000
buf ds.b 7 ; to hold the decimal string
org $1500
ldd #test_dat
lds #$1500 ; initialize stack pointer
ldy #buf
cpd #0
bmi negate ; branch if the value is negative
bne normal ; branch if the value is positive
movb #$30,buf
clr buf+1 ; terminate the string
bra done
negate coma ; when negative, complement the number
comb ; "
addd #1 ; "
movb #$2D,1,y+ ; add minus sign and move pointer
normal movb #0,1,-sp ; push a NULL into the stack
loop ldx #10
idiv
addb #$30 ; convert to ASCII code
pshb ; push into stack
cpx #0 ; is the quotient zero?
beq reverse
xgdx ; put quotient back to B
bra loop
reverse tst 0,sp
beq done
movb 1,sp+,1,y+ ; pop one byte and store it in buffer
bra reverse
done swi
end
Answer 3) Program :
out_buf ds.b 2 ; holds the converted binary value
buf2 ds.b 1 ; holds a zero
buf1 ds.b 1 ; holds the current digit value
sign ds.b 1 ; holds the sign of the number
error ds.b 1 ; indicates the occurrence of illegal character
org $1500
clr sign
clr error
clr out_buf
test_dat equ -2478
org $1000
buf ds.b 7 ; to hold the decimal string
org $1500
ldd #test_dat
lds #$1500 ; initialize stack pointer
ldy #buf
cpd #0
bmi negate ; branch if the value is negative
bne normal ; branch if the value is positive
movb #$30,buf
clr buf+1 ; terminate the string
bra done
negate coma ; when negative, complement the number
comb ; "
addd #1 ; "
movb #$2D,1,y+ ; add minus sign and move pointer
normal movb #0,1,-sp ; push a NULL into the stack
loop ldx #10
idiv
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.