Sunday 16 December 2012

topic 4 - sub 3 : system calls and MIPS Assembly Languages Program Format

System Calls

MIPS programs can make system calls (syscall) by placing parameters in specified registers, depending on the call, and executing a trap instruction. Returned results are made available in other specified registers, also depending on the call. System call allows user(s) to call upon the basic system functions. To use syscall, first set $v0 with the code of the function you want to call, then only use syscall. The exact codes available may depend on the specific system used, but the following are examples of common system calls.


                                                        Table : System call functions


* In SPIM, exception handler is used like :

li   $v0, code     # Load $v0 with the "system call code"
                         # number of an OS service
.........                # Put parameters for the service in
.........                # registers $a0, $a1
syscall               # Invoke the operating system
                         # Return value (if any) is in $v0 or $f0


* Examples of simple I/O for lab today :

# print an integer (Exp. number 7)
li   $a0, 7 --> loads the number we want to print into the first argument register
li   $v0, 1 --> stores the code for the print integer call into $v0
syscall      --> Executes system call

# print a null-terminated string
li   $v0, 4 --> loads service number in $v0
la $a0, prompt_string --> loads address of string to be printed into $a0
syscall

.data  --> the null-terminated string must be defined in data segment
prompt_string: .asciiz "Enter a value: "

# read in an integer
li   $v0, 5 --> loads service number in $v0
syscall --> the value entered by the user is returned in $v0
move $t0, $v0 --> store value entered into another register

# terminates execution of program
li   $v0, 10 --> loads service number in $v0
syscall




MIPS Assembly Languages Program Format

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. Sometimes can be referred as program code.


# Label (Optional)


The label portion of a statement must begin in column 1 and must end with a " : ". The " : " is not part of the label. It only serves to visually distinguish a new label definition from other program elements.

Each label represents a memory address in assembly language. It could be the address of data or the adddress of an instruction (i.e. labels can appear in both .text and .data sections).

A label represents the address of the instruction or data element that immediately follows it, whether it follows on the same line or a subsequent line.

Labels defined in a .data section are like variable names in HLLs, and follow the same naming rules. They must begin with a letter or underscore, and can contains letters, underscores, and digits.

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.
Example:

* main :  sll     $0,$0,0
              sll     $0,$0,0
              sll     $0,$0,0
              sll     $0,$0,0
              j       main         # jump to the point labeled main
              addiu $8,$8,1



# Opcode (Operational Code)

Opcode is the portion of a machine language instruction that specifies the operation to be performed. Their specification and format are laid out in the instruction set architecture of the processor.

Opcode is the field that denoted the basic operation and format of an instruction. Mnemonic language is used rather than hexadecimal code for the purpose of simplicity and readability.

These tables list the available operations in MIPS. For each instruction, the 6-bit opcode or function is shown.





# Operand

Operand may contain registers, shift amount, label to jump into and constant or address.

  • Specify the data required by the operation
  • Operands can be registers, memory variables, or constants
  • Most instructions have three operands
Example:
               addiu   $t0,$t0,1    # increment $t0



Comment

In MIPS assembly, comments are denoted with a number sign "#". Everything after that sign on the same line is a comment and is skipped by the assembler's lexer.

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.

  • 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.
  • 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 excution 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.
Example : 

label        Section/code           Comments     
                  
               .data                       # variable declaration
               .text                        # Code section
main:                                      # indicates start of code
                ori   $8,$0,0x2       # next instruction



GOH MENNING
B031210149

5 comments:

  1. thank you for sharing.. The details are quite good and understanding

    ReplyDelete
  2. i can understand well.^^

    ReplyDelete
  3. erm!! not bad..keep it

    ReplyDelete