Transcript Tutorial 4

More on Assembly
CS 210 Tutorial 4
Detail of Echo program























just specifes the entry point for the program
(in other words, where the program starts executing).
specify
just
specify
that
the
that
code
we void
are
in the
creating
specified
a block
file args)
isofimported
code called
(included)
main,
Like:
public
static
main(€String[]
using some definitions in a block
called register that specifies the register numbers for the symbolic names a0, a1, v0,
etc, and using
some definitions in a block called CALLSYS that specifies the values of
CALL_PAL_CALLSYS,
CALLSYS_GETCHAR, CALLSYS_PUTCHAR, etc.
entry main.enter;
import "../IMPORT/callsys.h";
block main uses CALLSYS {
code {
public enter:
{
loop:
ldiq $a0, CALLSYS_GETCHAR;
call_pal CALL_PAL_CALLSYS;
blt $v0, end;
mov $v0, $a1;
ldiq $a0, CALLSYS_PUTCHAR;
call_pal CALL_PAL_CALLSYS;
br loop;
end:
}
{
clr $a1;
ldiq $a0, CALLSYS_EXIT;
call_pal CALL_PAL_CALLSYS;
}
} code
} block main
Detail of Echo program








The lines
code {………..
……………….} code
just specify that we are defining code (instructions), rather than data.
The assembled bit patterns
are placed in the section of memory used for code.
The line
public enter:
labels some code, with the name “enter”. This code can be referred to
outside the block, as main.enter.
Detail of Echo program: Label


The lines
{











loop:
ldiq $a0, CALLSYS_GETCHAR;
call_pal CALL_PAL_CALLSYS;
blt $v0, end;
mov $v0, $a1;
ldiq $a0, CALLSYS_PUTCHAR;
call_pal CALL_PAL_CALLSYS;
br loop;
end:
}
represent the real work.
Detail of Echo program








The line
ldiq $a0, CALLSYS_GETCHAR;
loads the constant value CALLSYS_GETCHAR (whatever that has been
defined to be in the block CALLSYS, in “../IMPORT/callsys.h”) into
register a0.
The line
call_pal CALL_PAL_CALLSYS;
then makes a request (rather like a function invocation) to the operating
system to do something
(read a character from the keyboard). The operating system uses the
value in register $a0 todetermine what action to perform (in this case
read a character), and returns the result in register $v0.
Other parameters to system calls may be passed in registers $a1, $a2,
$a3, ...
Detail of Echo program












The lines
mov $v0, $a1;
ldiq $a0, CALLSYS_PUTCHAR;
call_pal CALL_PAL_CALLSYS;
move the character from register v0 into register a1, load the constant
value CALLSYS_PUTCHAR into register $a0 (to specify that the action is
to write a character), and makes a request to the operating system (to
write the character to the screen).
The lines
{
clr $a1;
ldiq $a0, CALLSYS_EXIT;
call_pal CALL_PAL_CALLSYS;
}
Cause the program to terminate and control to be returned to the operating system.
Assembly instruction syntax

Ex:



addq $t0, $t1, $t3;
subq $t0, 23, $t4;
There are five kinds of operands. The opcode determines the number and kind of
legal operands: Register, Unsigned 8 bit constant, Register, Brand operation,
Unsigned 26 bit constant.

Register: The operand represents a source or destination register. It is written
as “$register”, for example, $a0, $v0.

Unsigned 8 bit constant: The second operand of an integer operate
instruction can be of this form. The constant is written directly, without any
additional annotation, for example 23 in the above subq instruction.
Assembly instruction syntax

Memory address: The operand represents a memory address,
computed as a displacement (offset) from a base register. It is written
as “displacement($register)”, and means the displacement + contents
of integer register $register. The displacement is a signed 16 bit
integer. The displacement may be omitted if it is 0, allowing the
notation “($register)”. If the register is $zero (register 31), which always
contains 0, then the operand can be written with just the displacement.
For example, we can write 24($t0). The notation ($t0) is an abbreviation
for 0($t0), and the notation 1234 is an abbreviation for 1234($zero).
Displacement operands can only be used in load and store
instructions.

Branch destination: The operand represents a destination address for a
branch instruction. It is written directly as the destination address, but
is stored as a displacement from the address just after the branch
instruction. The last two bits of the displacement are not stored in the
instruction, because they are always 0.

• Unsigned 26 bit constant: The operand of a special instruction is of
this form. For example, PALL_PAL_CALLSYS in a call_pal instruction.
Load and store instructions







To operate on memory values, we must first load the source data from
memory, perform the computation, then store the result back in memory.
opcode $regA, displacement($regB);
Integer load instructions load a number of bytes starting at the specified
address
ldq (load quadword) loads the 8 bytes corresponding to a quadword,
starting at the memory address, into register.
ldbu (load byte unsigned) loads a single byte from the memory address,
into the low byte of register, making the high 7 bytes zero.
stq…
stqu…
Run DataRep2












Exercise DATAREP2
Suppose we have memory
0x1000000 0x123456789abcdef0
0x1000008 0x0000000000000000
0x1000010 0x0000000000000000
ldiq $t0, 0x1000000;
ldq $t1, ($t0);
stb $t1, 8($t0);
ldbu $t2, 2($t0);
sll $t2, 56, $t3;
sra $t3, 56, $t4;
stq $t4, 16($t0);