*************************************** * * * * * CS273 HW6 SOLUTION * * * * * *************************************** *======================================================== * 1. Justification of the problem *======================================================== * For an unsigned two-byte binary constant, the maximum * is 1111111111111111 in binary and 65535 in decimal. * This decimal number has 5 (BCD) digits. Representing * each digit with 4 bits, we need 5x4=20 bits to represent * the packed BCD digits in binary. In other words, 3 bytes * (as specified in the problem) are enough to store the * packed BCD equivalent in the memory. * * *======================================================== * 2. Detailed Algorithm *======================================================== * Input: an unsigned two-byte binary constant D * (e.g. 1111111111111111) * Output: its Packed BCD equivalent (e.g. 65535), * in N=5 succeesive BYTES (each taking ONE byte * instead of 4 bits) * * 1. initial all the 5 BCD bytes (temp ~ temp+4) as zero * 2. for y = N-1 to 0 do * if D < 10 then * store the lower byte of D into location temp+y * else * divide D by 10 * let D contain the quotient * store the lower byte of the reminder into location temp+y * * (Note: since each reminder is less than 10, only the lower 4 bits of * each byte were used.) *======================================================== * 3. Store the result in the required structure *======================================================== * digit: 6 5 5 3 5 * index: 4 3 2 1 0 * construct 3 bytes content, we get 06 55 35 * * *============================================= * 4. Assembly Code *============================================= N equ 5 *we have at most 5 BCD digits org $0000 temp rmb N *temp bytes, one for each digit output rmb 3 *output has 3 bytes org $f800 input fdb $ffff main * 1. initial the output bytes and all the temp 5 BCD bytes as zero * (Y is ultimately set to temp+N-1) ldaa #0 staa output staa output+1 staa output+2 ldy #temp ini_tmp clr 0,y inca cmpa #N beq conv iny bra ini_tmp conv ldd input *load d with the input number * 2. for y = N-1 to 0 do loop cpy #temp blo store cmpd #10 bhs next *if d >= 10, not done yet stab 0,y *if d < 10, done bra store next * divide by 10 ldx #10 idiv *after this, d holds the reminder, * while x holds the quotient stab 0,y *store the BCD digit xgdx *exchange x and d so that d holds the quotient dey *move on to the next digit bra loop store * Store the result in the required structure ldy #temp * construct the third byte of output ldaa 4,y ldab 3,y lslb lslb lslb lslb aba staa output+2 * construct the second byte of output ldaa 2,y ldab 1,y lslb lslb lslb lslb aba staa output+1 * construct the first byte of output ldaa 0,y staa output eloop bra eloop end main