read_int.s 885 Bytes
Newer Older
Benoit Perrot's avatar
Benoit Perrot committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
### isdigit(c)
	.text
isdigit:
	li	$v0, 0
	blt	$a0, 0x30, Lisdigit_end	# $a0 < '0' then return false
	bgt	$a0, 0x39, Lisdigit_end	# $a0 > '9' then return false
	li	$v0, 1			# return true
Lisdigit_end:
	jr	$ra

### getc()
	.data
Sgetc_c:
	.asciiz " "
	.text
getc:
	la	$a0, Sgetc_c
	li	$a1, 2
	li	$v0, 8
	syscall
	la	$t0, Sgetc_c
	lb	$v0, ($t0)
	jr	$ra

### read_int(first)
	.text
read_int:
	move	$s7, $ra		# return address in $s7
	
	sub	$s0, $a0, 0x30
Lread_int_loop:
	jal	getc
	sub	$s1, $v0, 0x30
	move	$a0, $v0
	jal	isdigit
	move	$v1, $v0
	beqz	$v0, Lread_int_loop_end

	li	$t0, 10
	mul	$s0, $s0, $t0
	addu	$s0, $s0, $s1

	j	Lread_int_loop
Lread_int_loop_end:
	move	$v0, $s0
Lread_int_end:
	jr	$s7

### read_int('2') < "097"
	.data
eol:
	.asciiz "\n"

	.text
main:
	li	$a0, 0x32
	jal	read_int
	move	$a0, $v0
	li	$v0, 1
	syscall
	la	$a0, eol
	li	$v0, 4
	syscall
	
	li	$v0, 10
	syscall