5.7 Các chương trình xử lý xuất nhập
SPIM cung cấp các hàm hệ thống dùng để xử lý các thao tác xuất nhập sau:
Công việc
Mã trong $v0
Tham số
Giá trị trả về
print integer
1
$a0 == integer
print float
2
$f12 == float
print double
3
($f12, $f13) == double
print string
4
$a0 == address of string
read integer
5
$v0 == integer
read float
6
$f0 == float
read double
7
($f0, $f1) == double
read string
8
$a0 == buffer address
$a1 == buffer length
allocate memory
9
$a0 == number of bytes
$v0 == address
exit 10
Sau đây là ví dụ sử dụng hàm hệ thống để in chuỗi ký tự và kết thúc chương trình:
Thiết lập các tham số trong menu Simulator -> Settings:
Bare Machine OFF, Allow Pseudo Instructions ON, Load Trap File ON, Delayed
Branches ON, Delayed Loads ON, Mapped IO OFF, Quiet OFF
Ví dụ sau in ra mẫu thư nhắc nhở trả sách một cách tự động sau khi người dùng nhập tên
người mượn sách trả trễ:
# hello.asm
#
.text
.globl main
main:
li $v0,4 # code 4 == print string
la $a0,string # $a0 == address of the string
syscall # Invoke the exception handler.
li $v0,10 # code 10 == exit
syscall # Halt the program.
.data
string: .asciiz "Hello SPIM!\n"
# end of file
5.8 Dùng stack tính toán biểu thức
Thanh ghi stack pointer ($sp) dùng để quản lý stack. Thanh ghi này chỉ đến phần tử trên
đỉnh ở vùng nhớ stack. Khi chương trình bắt đầu chạy, $sp có giá trị khởi tạo
0x7FFFFFFC. Vùng nhớ stack mở rộng xuống dưới đồng nghĩa giá trị thanh ghi $sp
giảm đi.
Tác vụ PUSH phần tử vào stack tiến hành hai việc:
• Thứ nhất, thay đổi giá trị thanh ghi $sp để trỏ đến phần tử đỉnh mới
• Thứ hai, lưu giá trị vào vị trí đỉnh mới
Tương tự, tác vụ POP phần tử ra stack tiến hành hai việc:
• Thứ nhất, lưu giá trị phần tử đỉnh stack vào biến
• Thứ hai, thay đổi giá trị thanh ghi $sp trỏ đến phần tử đỉnh mới
# overdue.asm
.text
.globl main
main:
# get patron name
li $v0,4 # print prompt
la $a0,prompt #
syscall
li $v0,8 # code 8 == read string
la $a0,name # $a0 == address of buffer
li $a1,24 # $a1 == buffer length
syscall # Invoke the operating system.
# print the letter
li $v0,4 # print greeting
la $a0,letter #
syscall
li $v0,4 # print body
la $a0,body #
syscall
li $v0,10 # exit
syscall
.data
prompt: .asciiz "enter name, followed by comma-enter: "
letter: .ascii "\n\nDear "
name: .space 24
body: .ascii "\nYour library books are way\n"
.ascii "overdue. Please return them\n"
.ascii "before we give your name\n"
.ascii "to the enforcement squad.\n"
# end of file
Thiết lập các tham số trong menu Simulator -> Settings:
Bare Machine OFF, Allow Pseudo Instructions ON, Load Trap File ON, Delayed
Branches ON, Delayed Loads ON, Mapped IO ON, Quiet OFF
# POP the item into $t0:
lw $t0,($sp) # Copy top the item to $t0.
addiu $sp,$sp,4 # Point to the item beneath the old top.
# PUSH the item in $t0:
addiu $sp,$sp,-4 # point to the place for the new item,
sw $t0,($sp) # store the contents of $t0 as the new top.
# Evaluate the expression ab - 12a + 18b - 7
#
# Settings: Load delays OFF; Branch delays OFF,
# Trap file ON; Pseudoinstructions ON
.globl main
main:
lw $t0,a # get a
lw $t1,bb # get b
mul $t0,$t0,$t1 # a*b
subu $sp,$sp,4 # push a*b onto stack
sw $t0,($sp)
lw $t0,a # get a
li $t1,-12 #
mul $t0,$t0,$t1 # -12a
subu $sp,$sp,4 # push -12a onto stack
sw $t0,($sp)
lw $t0,bb # get b
li $t1,18 #
mul $t0,$t0,$t1 # 18b
subu $sp,$sp,4 # push 18b onto stack
sw $t0,($sp)
Chia sẻ với bạn bè của bạn: |