   Next: 5 Bit Manipulation and Up: A Laboratory Manual for Previous: 3 Implementing Control Structures

# 4.1 Goal

To cover the SPARC operations related to multiplication and division.

# 4.2 Objectives

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

• The signed and unsigned multiplication and division operations,

# 4.3 Discussion

In this lab we consider the SPARC operations related to integer multiplication and division. We begin by considering the signed integer multiplication and division operations.

## 4.3.1 The multiplication and division operations

The integer multiplication operations multiply two 32-bit source values and produce a 64-bit result. The most significant 32 bits of the result are stored in the Y register (%y) and the remaining 32 bits are stored in one of the integer registers. Figure 4.1 illustrates the use of the Y register during a multiplication operation. Figure 4.1: Integer multiplication

The integer division operations divide a 32-bit value into a 64-bit value and produce a 32-bit result. The Y register provides the most significant 32 bits of the 64-bit dividend. One of the source values provides the least significant 32 bits, while the other provides the 32 bit divisor. A implementation of the SPARC may optionally store the remainder in the Y register. ISEM does not store the remainder in the Y register, so we will adopt this convention in our presentation. Figure 4.2 illustrates the use of the Y register during the division operation. Figure 4.2: Integer division

Table 4.1 summarizes the assembly language syntax for the integer multiplication and division operations provided by the SPARC. Like the addition and subtraction operations, the multiplication and division operations have two assembly language formats: one that uses registers for both source operands and another that uses a register and a small constant value for the source operands. Table 4.1: Assembly language formats for the integer multiplication and division operations

Table 4.2 summarizes the names for the signed and unsigned integer multiplication and division operations. Note that each operation has two SPARC operations: one that affects bits in the condition code register (e.g., smulcc), and another that does not (e.g., smul). Example 4.1 illustrates the use of these operations. Table 4.2: The signed and unsigned integer multiplication and division operations

Example:   Write a SPARC program to evaluate the statement a = (a*b)/c. In writing this code you should assume that a, b, and c are signed integer values and that all results can be represented in 32 bits.

```        .data
a:      .word   0x42
b:      .word   0x43
c:      .word   0x44

.text
start:  set     a, %r1
ld      [%r1], %r2
set     b, %r1
ld      [%r1], %r3
set     c, %r1
ld      [%r1], %r4

smul    %r2, %r3, %r2   ! a* b --> %y, %r2
sdiv    %r2, %r4, %r2   ! %y, %r2 / c --> %r2

set     a, %r1
st      %r2, [%r1]      ! %r2 --> a

end:    ta      0
```

The signed and unsigned operations are distinguished by the way they interpret their operands. The signed operations interpret their source operands as signed integers and produce signed integer results. The unsigned operations interpret their source operands as unsigned integers and produce unsigned integer results.

## 4.3.2 Updating the condition code bits

The operations that have names ending in ``cc'' update the bits in the condition code register. The integer multiplication operations (smulcc and umulcc) always clear the V (overflow) and C (carry) bits of the condition code register. In addition, these operations update the N (negative) and Z (zero) bits of the condition code register. Although the multiplication operations produce a 64-bit result, updates to the N and Z flags are only based on the least significant 32 bits of the result. Like the multiplication operations, the division operations (sdivcc and udivcc) also clear the C bit in the condition code register. In addition, the division operations update the N, Z, and V bits in the condition code register based on the value of the 32-bit result.

## 4.3.3 Examining and setting the Y register

Note that the code presented in Example 4.1 does not need to examine or set the value in the Y register. The multiplication sets the Y register and the division uses the value set by the multiplication. In many other cases you will need to examine or set the contents of the Y register. In particular, you may need to examine the contents of the Y register after a multiplication or set the contents of the Y register before a division. You can use the (synthetic) mov operation introduced in Lab 2 to copy the contents of the Y register to an integer register or vice versa. You can also use the mov operation to set the contents of the Y register. Table 4.3 summarizes the mov operation as it applies to the Y register. The first format copies the contents of the Y register to an integer register. The second format copies the contents of an integer register to the Y register. The third instruction format stores a small integer constant into the Y register. Table 4.3: The mov operation applied to the Y register.

When you store a value into the Y register (using a mov instruction), it takes three instruction cycles before the Y register is actually updated. This means that you need to make sure there are at least three instructions between an instruction that uses the Y register as a destination and an instruction that uses the value stored in the Y register.

SPARC Convention: Writing to the Y register
Remember, always make sure that there are at least three instructions between any instruction that writes to the %y register and an instruction that uses the value in the %y register.

Example: Write a SPARC assembly language fragment to evaluate the statement a = (a+b) / c. Again, you should assume that a, b, and c are signed integers and that all results can be represented in 32 bits.

```        .data
a:      .word   0x42
b:      .word   0x43
c:      .word   0x44

.text
start:  mov     %r0, %y         ! clear the Y register - THERE MUST BE AT
! LEAST 3 INSTRUCTIONS BETWEEN THE MOV AND
! SDIV INSTRUCTIONS
set     a, %r1
ld      [%r1], %r2
set     b, %r1
ld      [%r1], %r3
set     c, %r1
ld      [%r1], %r4

add     %r2, %r3, %r2   ! a + b --> %r2
sdiv    %r2, %r4, %r2   ! %r2 / c --> %r2

set     a, %r1
st      %r2, [%r1]      ! %r2 --> a
end:    ta      0
```

## 4.3.4 The multiply step operation

Prior to Version 8 the SPARC did not have integer multiplication or division operations. These operations had to be performed using more primitive operations. To simplify integer multiplication, earlier versions of the SPARC provided a multiply step operation, ``mulscc''. We will consider this operation in Lab 13 when take a closer look at integer arithmetic on the SPARC.

# 4.4 Summary

In this lab we have covered the integer multiplication and division operations provided by the SPARC. As with the other arithmetic operation (add and sub), there are versions of the multiplication and division operations that update the condition code bits and other multiplication and division operations that do not alter the condition code bits.

All of the multiplication and division operations operations use a special purpose register, the Y register (%y). You can use the mov operation to examine and set the contents of the Y register.

# 4.5 Review Questions

1. How is the Y register used in the integer multiplication operations?
2. How is the Y register used in the integer division operations?

# 4.6 Exercises   Next: 5 Bit Manipulation and Up: A Laboratory Manual for Previous: 3 Implementing Control Structures

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