0% found this document useful (0 votes)
21 views20 pages

Assembly Language

The document discusses assembly language and system calls in MIPS assembly. It provides examples of common system calls for input/output like reading integers, characters and strings from standard input and printing integers, characters and strings to standard output. It also discusses syscalls for file operations like opening, reading, writing and closing files.

Uploaded by

AMINA ATTA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views20 pages

Assembly Language

The document discusses assembly language and system calls in MIPS assembly. It provides examples of common system calls for input/output like reading integers, characters and strings from standard input and printing integers, characters and strings to standard output. It also discusses syscalls for file operations like opening, reading, writing and closing files.

Uploaded by

AMINA ATTA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

assembly language

1
syscalls
• Syscalls are operations defined within the assembler
(not the processor)
• The syscall operation is dependent on the value in
$v0
• Read operations store the value back in $v0

week04-3.ppt 3

04/24/24
syscalls
Integers

li $a0,100
li $v0,1 # print an integer in $a0
syscall

li $v0,5 # read an integer into $v0


syscall
syscalls
Characters

li $a0,’a’
li $v0,11 # print a character in $a0
syscall

li $v0,12 # read a character into $v0


syscall
syscalls
Strings

li $v0,4 # print an ASCIIZ string at $a0


la $a0,msg_hello
syscall
System calls – print_str

.data
str: .asciiz “Hello World”

.text
main:
li $v0, 4 # code for print_str
la $a0, str # argument
syscall # executes print_str
Inclass excercise

System calls – print_str


© PG/HC Programming 5JJ70 pg 9
Inclass excercise
Ask the user to enter an integer value, and then print the result
of doubling that number. Use the add instruction
Short Answer

© PG/HC Programming 5JJ70 pg 11


© PG/HC Programming 5JJ70 pg 12
syscalls
Others

li $v0,10 #exit
syscall
Example

• Write an assembly program to prompt the user for two numbers and
print the sum of the two numbers

14
example 1: add two numbers

Assembler directive
starts with a dot
.text # Code area starts here
main:
li $v0, 5 # read number into $v0
syscall # make the syscall read_int
move $t0, $v0 # move the number read into $t0

li $v0, 5 # read second number into $v0


syscall # make the syscall read_int
move $t1, $v0 # move the number read into $t1
instruction: system call
add $t2, $t0, $t1

move $a0, $t2 # move the number to print into


$a0
li $v0, 1 # load syscall print_int into $v0
syscall #

# end of main

© PG/HC Programming 5JJ70 pg 15


© PG/HC Programming 5JJ70 pg 16
© PG/HC Programming 5JJ70 pg 17
Inclass excercise
Write a program to sum all the integers between 1 and 5. [ 1, 5 ]
example 2: sum N numbers
# Input: number of inputs, n, and n integers; Output: Sum of integers
.data # Data memory area.
prmpt1: .asciiz "How many inputs? "
prmpt2: .asciiz "Next input: "
sumtext: .asciiz "The sum is "
.text # Code area starts here
main: li $v0, 4 # Syscall to print prompt string
la $a0, prmpt1 # li and la are pseudo instr.
syscall
li $v0, 5 # Syscall to read an integer
syscall
move $t0, $v0 # n stored in $t0

li $t1, 0 # sum will be stored in $t1


while: blez $t0, endwhile # (pseudo instruction)
li $v0, 4 # syscal to print string
la $a0, prmpt2
syscall
li $v0, 5
syscall
add $t1, $t1, $v0 # Increase sum by new input
sub $t0, $t0, 1 # Decrement n
j while

endwhile: li $v0, 4 # syscal to print string


la $a0, sumtext
syscall
move $a0, $t1 # Syscall to print an integer
li $v0, 1
syscall
li $v0, 10 # Syscall to exit
syscall © PG/HC Programming 5JJ70 pg 19
syscall examples
Trap
Service Input Output
code
print_int $v0 = 1 $a0 = integer to print prints $a0 to standard output
print_float $v0 = 2 $f12 = float to print prints $f12 to standard output
print_double $v0 = 3 $f12 = double to print prints $f12 to standard output
print_string $v0 = 4 $a0 = address of first character prints a character string to standard output

read_int $v0 = 5 integer read from standard input placed in $v0

read_float $v0 = 6 float read from standard input placed in $f0

read_double $v0 = 7 double read from standard input placed in $f0

read_string $v0 = 8 $a0 = address to place string, $a1 = max string length reads standard input into address in $a0
$v0= address of allocated memory
sbrk $v0 = 9 $a0 = number of bytes required
Allocates memory from the heap

exit $v0 = 10

print_char $v0 = 11 $a0 = character (low 8 bits)

read_char $v0 = 12 $v0 = character (no line feed) echoed

$a0 = full path (zero terminated string with no line feed),


file_open $v0 = 13 $v0 = file descriptor
$a1 = flags, $a2 = UNIX octal file mode (0644 for rw-r--r--)
$a0 = file descriptor, $a1 = buffer address, $v0 = amount of data in buffer from file
file_read $v0 = 14
$a2 = amount to read in bytes (-1 = error, 0 = end of file)
$a0 = file descriptor, $a1 = buffer address, $v0 = amount of data in buffer to file
file_write $v0 = 15
$a2 = amount to write in bytes (-1 = error, 0 = end of file)

file_close $v0 = 16 $a0 = file descriptor © PG/HC Programming 5JJ70 pg 20

You might also like