To cover the bit manipulation operations provided by the SPARC and the character I/O traps provided by ISEM.
After completing this lab, you will be able to write assembly language programs that use:
In this lab we introduce the bit manipulation operations of the SPARC. In particular, we consider the logical operations and, or, and xor. In addition, we consider the synthetic operation not. We follow this with a discussion of the shift operations sll, srl, and sra. The lab ends with a short discussion of the character I/O facilities provided by ISEM.
Table 5.1 summarizes the bitwise operations of the SPARC. Like the other data manipulation operations (e.g., add, sub, smul, sdiv, etc.), there are two instruction formats for each of the bitwise operations. Both formats use three explicit operands--two source operands and a destination operand. In the first format, both of the source operands are in registers. In the second format, one of the source operands is in a register, the other is a small constant value. This constant may be positive or negative; however its 2's complement representation must fit in 13 bits--the SPARC does sign extend this value. Examples 5.1 and 5.2 illustrate uses of the bitwise operations.
Table 5.1: Bitwise operations
.data a: .word 0x42 b: .word 0x43 c: .word 0x44 d: .word 0x45
.text start: set a, %r1 ld [%r1], %r2 ! a --> %r2 set b, %r1 ld [%r1], %r3 ! b --> %r3 set c, %r1 ld [%r1], %r4 ! c --> %r4 set d, %r1 ld [%r1], %r5 ! d --> %r5
and %r2, %r3, %r2 ! a & b --> %r2 or %r4, %r5, %r4, ! c $|$ d --> %r4 xor %r2, %r4, %r2 ! %r2 $$ %r4 --> r2
set a, %r1 st %r2, [%r1] ! %r2 --> a end: ta 0
.data n: .word 0xaaaaaaaa
.text start: set n, %r1 ld [%r1], %r2 ! n --> %r2
andn %r2, 0x1fe0, %r2 ! %r2 & $$0x1fe0 --> %r2
st %r2, [%r1] ! %r2 --> n end: ta 0
In this case, the mask (0x1fe0) can be represented using 12 bits and, as such, we can use an andn instruction with an immediate value.
Like the other data manipulation operations, there are separate bitwise operations that update the condition code register after they perform the specified operation. Table 5.2 summarizes the collection of bitwise operations that set the condition codes. The names for these operations have a suffix of ``cc'' to indicate that they update the bits in the condition code register.
Table 5.2: Bitwise operations--condition code versions
Bitwise inversion is provided by a synthetic operation. Table 5.3 summarizes this operation. Bitwise inversion does not affect the bits in the condition code register.
Table 5.3: Bitwise inversion
Table 5.4 summarizes the shift operations of the SPARC. The SPARC provides two instruction formats for each of the shift operations. The shift operations provide a mechanism for moving every bit in a value to the left or right by a specified number of positions. The shift operations do not affect the bits in the condition code register.
Table 5.4: The shift operations
As shown in Table 5.4, the shift operations have two source operands. The first operand specifies the value to be shifted. This value must be stored in an integer register. The second source operand specifies the amount of the shift. This operand may be stored in an integer register or it may be a small constant value. The processor only uses the least significant 5 bits of the second source operand (and ignores the remaining bits of this operand). Example 5.3 illustrates the shift operations.
.data n: .word 0xaaaaaaaa
.text start: set n, %r1 ld [%r1], %r3 clr %r2 ! clear the result
loop: andcc %r3, 1, %r0 ! check the lowest bit be cont ! skip if zero nop ! (delay slot) inc %r2 ! increment the 1's count cont: srl %r3, 1, %r3 ! shift data right, creating a new low bit cmp %r3, 0 ! if %r3 == 0, we're done bne loop nop
end: ta 0
When you use isem-as, character values are delimited using single quotes. For example, 'B' denotes the value associated with the letter B. When it encounters a character value, isem-as converts the character value into its ASCII representation. As such, writing 'B' in an assembly language program is equivalent to writing 0x42 (or 66). You should use the notation that best expresses the intent of your code.
ISEM provides the user with a primitive form of character input/output using the trap mechanism. We will discuss the trap mechanism in detail in Lab 16. For now, we note that the following instructions can be used to get and put characters from the standard I/O devices.
ta 1 ! %r8 --> putchar ta 2 ! getchar --> %r8
The first of these instructions prints the character in the least significant byte of register %r8 (= %o0) to standard output and the second reads a character from standard input and places the result in the least significant byte of %r8, clearing the most significant 24 bits of this register. Example 5.4 illustrates the use of these I/O instructions.
.text start: ta 2 ! digit --> %r8 sub %r8, '0', %r8 ! convert from digit to number add %r8, 5, %r8 ! add 5 cmp %r8, 9 ! is the result greater than 9? ble one_dg nop
mov %r8, %r7 ! copy %r8 into %r7 set '1', %r8 ! the most significant digit must be '1' ta 1 ! '1' --> putchar mov %r7, %r8 ! restore %r8 sub %r8, 10, %r8
one_dg: ! %r8 holds a number less than 10 add %r8, '0', %r8 ! convert number to digit ta 1 ! print the least significant digit
In this lab we have introduced the bitwise and shift operations provided by the SPARC. In addition, we have introduced the notation used for character data and primitive mechanisms for character input and output. Example 5.5 illustrates all of these operations.
.data n: .word 0xaaaaaaaa
.text start: set n, %r1 ld [%r1], %r2 set 1 $<<$ 31, %r3 ! initialize the mask to start ! with the most significant bit set 32, %r4 ! number of bits to print
loop: andcc %r2, %r3, %g0 ! check for one be print ! branch on zero set '0', %r8 ! (delay slot)
set '1', %r8 ! must have been a one
print: ta 1 ! print %r8
deccc %r4 ! decrement count bg loop ! continue until count == 0 srl %r3, 1, %r3 ! shift mask right (bd)
end: ta 0