Reverse byte using assembly language

I am in the microprocessor class and we use the assembly language in Freescale CodeWarrior to program the 68HCS12 microcontroller. Our task this week is to restore the byte, so if the byte was 00000001, the output would be 10000000, or 00101011 - 11010100. We should use assembly language, and we were told that we can use rotations and shifts (but not limited to them!) For complete this task. I am really at a loss where I should start.

+7
source share
8 answers

If you can save a 256-byte extra code size, then a lookup table is probably the most efficient way to invert a byte to 68HCS12. But I'm sure this is not what your instructor expects.

For a “normal” solution, consider the data bit separately. Rotation and shifts allow you to move bits around. For the first solution, select eight bits (with bitwise "and" operations), move them to their destinations (shifts, rotation ...), then combine them again (with bitwise "or" operations "). It will not be the most efficient or simple implementation, but first you have to focus on getting the right result - optimization can wait.

+6
source

Tips: if you are doing a shift, one bit is reset and zero (possibly) is switched. Where does this shifted bit go? You need to transfer this to the other end of the destination address or memory address.

I am sure that 25 years ago I could do it in Z80 machine code without assembler :)

+8
source

Consider two registers as stacks of bits. What happens if you move one bit at a time from one to another?

+7
source

Of all, FIrst will work out an algorithm to do what you need to do. Express it as pseudocode or C or plain English or charts or whatever you like. Once you clear this conceptual hurdle, the actual implementation should be pretty simple.

Your processor probably has instructions that allow you to shift and / or rotate the register, possibly including a carry flag as an extra bit. These instructions will be very helpful.

+3
source

When you make a right shift, which low-order bit falls into the carry flag.

When you perform a rotation, the carry flag is used to fill in the freed bit of the result (LSB for ROL, MSB for ROR).

+3
source

It was a comment, but I thought WTH!

To save space on a 256-byte table, you can have a 16-byte table containing values ​​for four bits (nibbles) at a time. Then the algorithm will be

revval=(revdigit[inval&0x0f]<<4)| revdigit[inval>>4]; 

If I were a professor, I would certainly like the two parts where one shift is in indexing and the other outside.

+1
source

For example, if you have a byte number, the easiest way is

 mov al, 10101110 mov ecx, 8 

put 8 in ecx for the loop

 mov ebx, 0 

In bl, we will have a result, we will do ebx, only to see which is better

 loop1: sal al, 1; 

In the carry flag you now have the last bit on the left

 rcr bl, 1; 

now you add to bl what you have in

 loop loop1 

and that's all

+1
source

I also had to program this bit for university (for 8 bits). Here is how I did it:

 MOV AL, 10001011B ;set the value to test MOV CL, 7 MOV DH, 1 MOV DL, 0 loop1: PUSH AX AND AL, DH PUSH CX MOV CL, DL SHR AL, CL POP CX MOV BH, AL SHL BH,CL OR CH,BH DEC CL INC DL SHL DH, 1 POP AX CMP DL, 8 JE END JMP LOOP1 END: 

I have not commented on this, here is how it works: DH is 1 , which moves in byte as the first time: 00000001 ; second time 00000010 and so on. When you do AND with AL, you get 0 or something like 100 or 10000 , you need to shift it to the right to get it as 0 or 1 . Then put it in BH and go to the desired position, which is 7 for byte 0 , 6 for byte 1 and so on. Then OR to our final result and INC and DEC , which is necessary. Do not forget the conditional jumps and pop AX for the next cycle :)

The result will be in CH.

0
source

All Articles