Buscar

CS310-CH10 - Assembler Basics - Part2

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

*
*
Assembler Basics – Bit Manipulation
Western Illinois University
Department of Computer Science
CS310 – ST224
 
Computer Organization 
& Architecture 
Created by Dr. Leff Modified by Dr. Martins
*
*
Contents
Parallel bit manipulation
Shifting bits
Transplanting bits
*
*
Parallel Manipulation
In most assembly languages, including the Intel PC, there are commands to do pairwise logical operations between two words. 
That is, we can cause the machine to: 
take the logical "and" of the bit in position zero (leftmost) of a register with the bit in position zero of a memory location. It puts the result back in the bit at position zero of the register. 
take the logical "and" of the bit in position one of a register with the bit in position one of a memory location. It puts the result back in the bit at position one of the register. 
*
Turning Bits OFF
*
*
*
Example: Parallel Manipulation
After the and is performed, register ax would contain: 
*
*
Parallel Manipulation
Observe that where there are one's in the mask,Ax stayed the same. 
Where there were zero's in the mask, Ax was turned off.
Therefore, when one wants to turn off certain bits in a word, and with a mask that has zero bits where we want to turn off, and one bits where we want the word to stay the same. 
*
*
Parallel Manipulation
For example, we could have the program:
 .code 
 startupcode 
 mov ax,n 
 and ax,mask1 
 mov b,ax 
n dw 0aaaah 
mask1 dw 00ffh 
b dw 
*
Turning Bits ON
*
*
*
Example: Parallel Manipulation
N OR MASK
*
*
Parallel Manipulation
Let's try this with the or statement. 
 mov ax,n 
 or ax, mask 
n dw aaaah 
mask dw 00ffh 
*
*
Parallel Manipulation
Observe that where there are one's in the mask,Ax was turned on.
 Where there were zero's in the mask, Ax remained the same. 
Therefore, when one wants to turn on certain bits in a word, or with a mask that has one bits where we want to turn on, and zero bits where we want the word to stay the same. 
*
Toggling bits
*
*
*
Parallel Manipulation
Example
*
*
Parallel Manipulation
What do you think would happen if we apply an assembler instruction called xor to the same two words? 
 mov ax,N 
 xor ax,mask 
N dw aaaah 
mask dw 00ffh 
*
*
Parallel Manipulation
The moral of the xor is: 
When one wants to toggle certain bits in a word, xor with a mask that has zero bits where we want to leave alone, and one bits where we want to toggle the bit. 
*
*
Shift
Again, the Intel 80xxx6 processor will shift a word pairwise either to the left or to the right. This feature corresponds to the shift register, about which we learned earlier.
There are two instructions: shl and shr to shift left and right. We use them as follows: shl ax, 3 ; shifts ax n positions to the left. shr ax,5 ; shifts ax n positions to the right. 
*
*
Shift
Recall that the cx register consists of CH and CL as its two eight-bit halves. Thus, the instructions above will erase previous contents of cx!. 
Recall, as we discussed earlier, that:
Shifting left by n positions multiplies by 2n. 
Shifting right by n divides the number by 2n. 
*
Transplanting Bits
*
*
*
Transplanting Bits
From time to time, we need to move a field of bits between one memory location and a receiving field in another one. Assume that we number from the right. For example, one might need to move positions four to seven of A to positions 8 to 11 of B. Assume A and B are as indicated: 
*
*
Transplanting Bits
 
Then, the program we desire should copy the red bits 
 of A to replace the red bits of B.
b0
b15
b4
LSB
*
*
Transplanting Bits - steps
There are several steps in such shifts: 
move the first memory location to ax for processing. 
move the second memory location to bx for processing. 
blank out all bits except the ones to be moved from ax 
blank out the receiving field in bx 
use a shift instruction to position the bits from ax in parallel with the receiving field. 
*
*
Transplanting Bits
Use an or instruction to move the bits. Earlier, we were careful to zero any bits in ax outside of the field being moved. We were also careful to zero out the bits in the receiving field. Thus, the two fields won't interfere with each other. (In each column, one of the two registers will have zero, by design.) 
copy bx back to the receiving field.
*
*
Transplanting Bits
That gives us our program: 
; move bits from the second nibble of A (positions 
; 4 to 7 from the right) to the third nibble of B
; (positions 8 to 11 from the right) 
*
*
and ax,mask1
After the instruction, AX will hold:
Blank out all bits except the ones to be moved from ax 
*
*
 and bx,mask2 
Blank out the receiving field in bx
Mask2 dw 0f0ffh
After the instruction, BX will hold:
*
*
shl ax,cl (Shift left register ax)
Use a shift instruction to position the bits from ax in parallel with the receiving field. 
After this instruction, AX will hold
Register CL contains 4
*
*
or bx,ax
Use an or instruction to move the bits
After this instruction, BX will hold
The transplantation is concluded. We just need to save BX in variable b.
*
*
;move bits from memA (positions x to y ; to memB (positions m to n     mov ax,memA   	 mov bx,memB 	   and ax,mask1 	   and bx,mask2 	   mov cl,m - x 	   shl or shr ax,cl 	   or bx,ax 	   mov memB,bx     	 exitcode a	 dw 89abh b	 dw 1234h mask1 dw ; four hex-digit constant corresponding to positions x 
 ; to y on and all other bits off mask2 dw ; four hex-digit constant corresponding to positions x 
 ; to y off and all other bits on 
*
*
Template
;move bits from memA (positions x to y ; to memB (positions m to n     mov ax,memA   	 mov bx,memB 	   and ax,mask1 	   and bx,mask2 	   mov cl,m - x 	   shl or shr ax,cl 	   or bx,ax 	   mov memB,bx     	 exitcode a	 dw 89abh b	 dw 1234h mask1 dw ; four hex-digit constant corresponding to positions x 
 ; to y on and all other bits off mask2 dw ; four hex-digit constant corresponding to positions x 
 ; to y off and all other bits on 
*
Exercícios (para entregar)
Escreva um programa em ASM para ligar os bits 0,3,5 e 7 de uma palavra N na memória.
Escreva um programa em ASM para desligar os bits 2,4,5,8 do controlador da interface USART, que pode ser acessado através da variável U.
Escreva um programa para ligar os bits 3,5 e 7 e desligar os bits 2, 4 e 6, da variável X.
*

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Continue navegando