Tuesday, 3 December 2013

MIPS (System calls/MIPS Assembly Language Program Format/Data Representation) by LEE XUE WEN B031310149



SYSTEM CALLS

System call used to read or print values or strings from input/output window, and indicate program end. SPIM provides a small set of operating-system-like services through the system call (syscall) instruction. To request a service, program loads the system call code into register $v0 and the arguments into registers $a0Description: $\ldots$$a3 (or $f12 for floating point values). System calls that return values put their result in register $v0 (or $f0 for floating point results).

System call function
Service
System call
code
Arguments
Result
print_int
1
$a0 = integer

print_float
2
$f12 = float

print_double
3
$f12 = double

print_string
4
$a0 = string

read_int
5

$v0 = integer
read_float
6

$f0 = float
read_double
7

$f0 = double
read_string
8
$a0 = buffer
$a1 = length

Sbrk(allocate memory)
9
$a0 = amount
$v0 = address
exit
10


print_char
11
$a0 = char

read_char
12

$v0 = char

















How to use SYSCALL system services
Step 1: Load the service number in register $v0.
Step 2: Load argument values, if any in $a0, $a1, $a2, or $f12 as specified.
Step 3: Issue the SYSCALL instruction.
Step 4: After call, return value is in register $v0 



MIPS ASSEMBLY LANGUAGES PROGRAM FORMAT
v  MIPS assembler program is a plain text file that made up of two types of statements:
·         Assembler directive that tells the assembler how to translate a program but cannot be translated into machine code.
·         Executable instruction, where upon execution will be translated into machine code and can be refferes as program code.

v  MIPS program have a format that comprises of four columns:

Column 1
Column 2
Column 3
Column 4
Label                                  Opcode                              Operand                           #Comment


Column 1: Label (Optional)
Ø  The labels component lists zero or more labels, each followed by a colon (:). A label is a string of alphanumeric characters, the first of which must be a letter of the alphabet. A label is used to mark a specific point in the program code. In this case, when instruction of BRANCH or JUMP call for a specific label, the program will execute from the point of the label is situated. For example:
Main: sll        $0, $0, 0
           sll        $0, $0, 0
           sll        $0, $0, 0
           sll        $0, $0, 0
           j           main                 #jump to the point labelled main
           addiu  $8, $8, 1


Column 2: OPCODE (OPERATION CODE)
Ø  Opcode is the field that denoted the basic operation and format of an instruction. Mnemonic language is used rather than gexadecimal code for the purpose of simplicity and readability.
Ø  Example of opcode are addiu (as in the sample code above), add mul, j, etc.


Colum 3: OPERAND
Ø  “Operand” – Input or Output values for an operations
Ø  Operand may contain:
§  Registers
§  Shift amount
§  Label to jump into
§  Constant or address


Column 4: Comment
Ø  Comment is anything that follows # on a line
Example:  # this is a comment
Ø  Block comments are formed from multiple line comments.
################################################################
This is a block comment
###############################################################
MIPS program structure is just a plain text file with data declarations and program code. The name of file should end in suffix .asm to be used with SPIM simulator. Data declaration section should be followed by program code section.

v  Data declarations are placed in section of program identified with assembler directive .data.
Ø  Variable names used in the program are declared with storage allocation set in main memory (RAM).
Ø  Revise data representation sub-chapter to identify which data type to use on the declaration.

v  Code is placed in section of program identified with assembler directive .text.
Ø  The section contains program code (instructions to be executed)
Ø  Starting point for code execution is from a certain label at the left column where the programmers usually use main:
Ø  Ending point of main code should use exit system call.

v  Top-level Directives:
•  .text
§    indicates that following items are stored in the user text segment, typically instructions
•  .data
§    indicates that following data items are stored in the data segment
•  .globl sym
§    declare that symbol sym is global and can be referenced from other files


v    Common Data Definitions:
•  .word w1, …, wn
§  store n 32-bit quantities in successive memory words
•  .half h1, …, hn
§    store n 16-bit quantities in successive memory halfwords
•  .byte b1, …, bn
§  store n 8-bit quantities in successive memory bytes
•  .ascii str
§    store the string in memory but do not null-terminate it
¨       strings are represented in double-quotes “str”
¨         special characters, eg. \n, \t, follow C convention
•  .asciiz str
§  store the string in memory and null-terminate it
•  .float f1, …, fn
§    store n floating point single precision numbers in successive memory locations
•  .double d1, …, dn
§  store n floating point double precision numbers in successive memory locations
•  .space n
§    reserves n successive bytes of space
•  .align n
§  align the next datum on a 2n byte boundary.
§  For example, .align 2 aligns next value on a word boundary.
§  .align 0 turns off automatic alignment of .half, .word, etc. till next .data directive






DATA REPRESENTATION
Data representation refers to how information is stored within the computer. In order to understand how to manipulate data and perform computations in MIPS program, first must understand how data is represented by a computer in a view of MIPS programming language.

v  CHARACTER REPRESENTATION
All represented by a computer is represented by binary sequences. A common non-integer to be represented is a character. Use standard encodings (binary sequences) to represent characters. A character can be represented with one byte. Many I/O devices work with 8-bit quantities. A standard code ASCII (American Standard for Computer Information Interchange) defines what character is represented by each sequence.
 For examples: 
0100 0001 is 41 (hex) / 65 (decimal). It represents 
'A' 
0100 0010 is 42 (hex) / 66 (decimal). It represents 
'B'
Different bit patterns are used for each different character that needs to be represented.
The code has some nice properties. If the bit patterns are compared, (pretending they represent integers), then 'A' < 'B'. This is good, because it helps with sorting things into alphabetical order.
Notes:
  • 'a' (0x61) is different with 'A' (0x41)
  • '8' (0x38) is different with the integer 8
    two's complement representation for 8: 
    0000 0000 0000 0000 0000 0000 0000 1000
    the ASCII character '8': 
    0011 1000
  • the digits:
    '0' is 48 (decimal) or 0x30
    '9' is 57 (decimal) or 0x39
Because of this difference between the integer representation for a character, and the character representation for a character, we constantly need to convert from one to the other.
The computer does arithmetic operations on two's complement integers (and often operations on unsigned integers). The computer has the ability to read in or print out a single character representation at a time. So, any time we want to do I/O, we're working with one character at a time, and the ASCII representation of the character. Yet, lots of the time, the data represents numbers (just consider integers, for now).

Part of what an assembler does is to assemble the ASCII bit patterns that had placed in memory. Here is a section of an assembly language program:
.acsiiz     “XYZ  xyz”
Here are the bit patterns that the assembler will produce in the object module:
58  59  5A  20  78  79  7A  00
The .asciiz part of the source code asked the assembler to assemble the characters between the quote marks into ASCII bit patterns. The numbers represent hexadecimal system, or the front of each number can be written as 0x. The first character, “A”, corresponds to the bit pattern 0x41. The second character, “B”, corresponds to the bit pattern 0x42. The fourth character, “ “ (space), corresponds to the bit pattern 0x20. The final bit pattern 0x00 (NUL) is used by the assembler to show the end of the string of characters.
An alternative way to write each character is as follows:

 
.byte  88  89  90  32  120  121  122  00


Example:
Suppose the name ‘Steven Swinnea’ is to be represented using ASCII character codes.  The internal representation is:
01010011011101000110010101110110
01100101011011100010000001010011
01110111011010010110111001101110
01100101011000010000000000000000
Each character requires 8-bits.  The bit patterns are easy for a computer to understand but is a bit too much for the human eye. 

A more compact representation can be obtained by converting each group of four bits to hexadecimal.

Hexadecimal (Base 16) Representation
Binary
Hexadecimal

Binary
Hexadecimal
0000
0

1000
8
0001
1

1001
9
0010
2

1010
A
0011
3

1011
B
0100
4

1100
C
0101
5

1101
D
0110
6

1110
E
0111
7

1111
F

St                            ev                           en                           S                              wi                           nn                           ea
5374                       6576                       656E                       2053                       7769                       6E6E                       6561

ASCII representation of characters
Dec
Hex
Char
Dec
Hex
Char
Dec
Hex
Char
32
20
SPACE
33
21
!
34
22
35
23
#
36
24
$
37
25
%
38
26
&
39
27
40
28
(
41
29
)
42
2A
*
43
2B
+
44
2C
,
45
2D
-
46
2E
.
47
2F
/
48
30
0
49
31
1
50
32
2
51
33
3
52
34
4
53
35
5
54
36
6
55
37
7
56
38
8
57
39
9
58
3A
:
59
3B
;
60
3C
61
3D
=
62
3E
63
3F
64
40
@
65
41
A
66
42
B
67
43
C
68
44
D
69
45
E
70
46
F
71
47
G
72
48
H
73
49
I
74
4A
J
75
4B
K
76
4C
L
77
4D
M
78
4E
N
79
4F
O
80
50
P
81
51
Q
82
52
R
83
53
S
84
54
T
85
55

86
56
V
87
57
W
88
58
X
89
59
Y
90
5A
Z
91
5B
[
92
5C
\
93
5D
]
94
5E
^
95
5F
-
96
60
`
97
61
a
98
62
b
99
63
c
100
64
d
101
65
e
102
66
f
103
67
g
104
68
h
105
69
i
106
6A
i
107
6B
k
108
6C
l
109
6D
m
110
6E
n
111
6F
o
112
70
p
113
71
q
114
72
r
115
73
s
116
74
t
117
75
u
118
76
v
119
77
w
120
78
x
121
79
y
122
7A
z
123
7B
{
124
7C
|
125
7D
}
126
7E
~
127
7F
DEL


Summary
Character data is at least as important as numeric data. Like numeric data, character data is represented using 0's and 1's. The most commonly used character representation (at least, in the US) is ASCII. However, Unicode is gaining popularity, and should eventually become the standard character set in programming languages.
v  NUMBER REPRESENTATION
Computers operate on binary numbers, in MIPS number is represented in decimal system or hexadecimal system. When need to insert number into a register, remember what kind of number system that wants to write in the program. For example, to load number into register $5, we have two ways to represent decimal 16 in MIPS:
1.       ori  $5,  $0,  16    #load number 16 into register $5
2.       ori  $5,  $0,  0x10    #load number 16 into register $5






FOUNDAMENTALS OF EMULATION
To learn  MIPS architecture we use emulator to understand the inside of the processor. Using an emulator, give many advantages over the actual hardware usage. The emulator runs on most common computers: Mac, Intel PC or even MIPS.
An emulator running on the processor it is emulating is often used for debugging. Working with emulator is more convenient then working with the real processor.





















No comments:

Post a Comment