Next: 6 Assembler DirectivesAssembler Up: A Laboratory Manual for Previous: 4 Multiplication and Division

# 5.1 Goal

To cover the bit manipulation operations provided by the SPARC and the character I/O traps provided by ISEM.

# 5.2 Objectives

After completing this lab, you will be able to write assembly language programs that use:

• The bitwise operations,
• The shift operations, and
• The character I/O traps provided by ISEM.

# 5.3 Discussion

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.

## 5.3.1 Bitwise operations

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

Example:   Write a SPARC program to evaluate the statement: .

```        .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
```

Example:   Write a SPARC program to clear bits 5 through 12 of the word n (bit 0 is the least significant bit).

```        .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.

Activity: Write a SPARC program to clear bits 5 through 13 of the word n.

## 5.3.2 Condition codes

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

## 5.3.3 Synthetic operation not

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

## 5.3.4 Shift operations

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.

Example:   Write a SPARC program to count the number of bits that are set (i.e., 1) in the memory location n (a variable). The result should be stored in %r2. In writing this code, you may use any of the remaining registers as temporaries.

```        .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
```

## 5.3.5 Character data and character I/O in ISEM

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.

Example:   Write a SPARC/ISEM program that reads a one digit number, adds five to the number and prints the result.

```        .text
start:  ta      2               ! digit --> %r8
sub     %r8, '0', %r8   ! convert from digit to number
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

ta      0
```

# 5.4 Summary

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.

Example:   Write a SPARC/ISEM program to print the binary representation of the unsigned integer in memory location n. In writing the code, you may use any of the registers as temporaries.

```        .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
```

# 5.5 Exercises

1. Write a SPARC program that compares the memory contents of the word pointed to by register to the contents of register on a bit-by-bit basis. For all bits i, if the value of bit i of is smaller than the value of bit i of , set bit i of to 1; otherwise, set bit i of to 0.
2. Write a SPARC program that distinguishes ASCII-coded hexadecimal digits from other bytes and which converts valid digit codes to the corresponding hexadecimal value. Only valid digit codes are to be converted. Leave invalid characters unaltered. Suppose that register holds the character on program entry, and that holds the result on program exit. Use register to report on validity. If valid, must equal 0; if invalid, must equal 1. Valid Digits, Their Codes and Values

3. Write a SPARC program to print the hexadecimal representation of an unsigned integer. The unsigned integer should be named n and declared in the data segment. Your program should finish by printing a newline.

Next: 6 Assembler DirectivesAssembler Up: A Laboratory Manual for Previous: 4 Multiplication and Division

Barney Maccabe
Mon Sep 2 20:51:56 MDT 1996