Princeton University
COS 217:  Introduction to Programming System

Precept 13:  SPARC Assembly Language Fundamentals

Purpose

Help you learn fundamental SPARC assembly language programming:  load and store instructions, arithmetic and logical instructions

Reading

Paul, Chapters 1, 2, 3, 4, 8, 9 (for several precepts)

Approach

Study many small C programs and corresponding hand-written assembly language programs

After studying each program, refer to summary sheets to reinforce and generalize the new material that the program illustrates

Example:  absval

Illustrates load and store instructions

See absval.c and absval.S

What it does

Reads an integer, and computes and writes its absolute value

How it works

Calls standard C function "abs"

The code

Just as instructions must be aligned on 4-byte boundaries, so must int variables

ld [%l0], %o0

Assembly language statement

Load 4 bytes from memory whose address is in %l0 into %o0

abs returns its value in %o0

st %o0, [%l0]

Assembly language statement

Store 4 bytes from %o0 into the memory whose address is in %l0

The abs function placed its return value into %o0

Note:  Could optimize the code by using a register instead of memory to store the absolute value

For now, assembly language programs will mimic the corresponding C programs as closely as possible

Later precept will discuss optimization

Introduced these assembly language features:

Function return values

Load and store instructions

ld
st

How to specify a memory address (inside brackets in ld and st instructions):

reg
reg + reg
reg + immed13   Handy for using stack (as we'll see)
reg - immed13   Handy for using stack (as we'll see)
immed13 + reg   Rarely used
immed13

Example:  toascii

Illustrates additional load instruction

See toascii.c and toascii.S

What it does

Reads a character, and writes its ASCII code

E.g., reads 'a', writes 96

How it works

Uses ldub and st instructions

The code

piAsciiCode must denote an address that is evenly divisible by 4

I.e. asciiCode must be stored in memory that is on a word boundary

Not so => runtime (bus) error when attempt to store

ldub [%l0], %l1

Assembly language statement

Load unsigned byte (not load double!!!)

Load an unsigned byte from the memory whose address is given in %l0 into %l1

Pads with zero; does not sign extend (see ldsb)

Note:  What if we used ld instead?

Would load 4 bytes beginning with pcChar => very large number

st %l1, [%l2]

Store 4 bytes from %l1 to the memory address contained in %l2

Note:  Good thing asciiCode is on a word boundary

Note:  Could optimize code

Introduced these assembly language features:

Load and store instructions

ldub

Example:  fromascii

Illustrates additional store instruction

See fromascii.c and fromascii.S

What it does

Reads an integer, and writes the character whose ASCII code is that integer

How it works

Uses stb instruction

The code

stb %l1, [%l2]

Assembly language statement

Store the low-order byte of %l1 into the memory whose address is given in %l2

What if we used st instead?

Would store 4 bytes from %l1 into the word whose first byte is at location pcChar

Subsequent ldub would load 0

Would stub work instead of stb?

Yes;  stb sign extends, stub does not

Introduced these assembly language features:

Load and store instructions

stb

Example:  rect

Illustrates arithmetic, logical, and shift instructions

See rect.c and rect.S

What it does

Reads rectangle's length and width, and computes and writes its perimeter

How it works

perimeter = 2 * (length + width)

The code

Note use of sll and add instructions

Introduced these assembly language features:

Shift instructions

sll

Arithmetic instructions

add

To multiply by powers of 2:  use sll

To multiply by other numbers:  call .mul (rarely used in systems programming)

To divide by powers of 2:  use sra

To divide by other numbers:  call .div (rarely used in systems programming)

Copyright © 2002 by Robert M. Dondero, Jr.