next up previous
Next: About this document Up: Ultrix System Procedures Previous: syscall Definitions

Example: syscall I/O

This example shows a MAL program which performs the same function as the preceding example using syscall instructions to invoke read() and write() directly instead of using jal instructions.


dec1% cat syscall.s
 #
 #  syscall.s: MAL program to illustrate syscall I/O functions
 #
	.data	
$$62:	.ascii	"Type something: "

	.text	
	.globl	main
 #  5	{ 
main:
	subu	$sp, 12
	sw	$31, 0($sp)
 #  6	  int n;
 #  7	  char c;
 #  8	  n = write(0,"Type something: ",16);
	li	$2, 4
	move	$4, $0
	la	$5, $$62
	li	$6, 16
	syscall
	sw	$2, 8($sp)
 #  9	  while (read(1,&c,1))
$32:	li	$2, 3
	li	$4, 1
	addu	$5, $sp, 4
	li	$6, 1
	syscall
	beq	$2, $0, $33
 #  10	    n = write(0,&c,1);
	li	$2, 4
	move	$4, $0
	addu	$5, $sp, 4
	li	$6, 1
	syscall
	sw	$2, 8($sp)
	b	$32
 #  11	}
$33:	move	$2, $0
	lw	$31, 0($sp)
	addu	$sp, 12
	j	$31

dec1% cc syscall.s
dec1% a.out
Type something: Hello World
Hello World
^D
dec1%




CS301 Class Account
Thu Dec 12 23:09:15 AST 1996