25.12.2012 Views

MIPS Assembly Language Programming

MIPS Assembly Language Programming

MIPS Assembly Language Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5.6. ATOI-4.ASM 79<br />

39<br />

40 mult $t2, $t4 # multiply $t2 by 10.<br />

41 mfhi $t5 # check for overflow;<br />

42 bnez $t5, overflow # if so, then report an overflow.<br />

43 mflo $t2 # get the result of the multiply<br />

44 blt $t2, $0, overflow # make sure that it isn’t negative.<br />

45<br />

46 sub $t1, $t1, ’0’ # t1 -= ’0’.<br />

47 add $t2, $t2, $t1 # t2 += t1.<br />

48 blt $t2, $0, overflow<br />

49<br />

50 b sum_loop # and repeat the loop.<br />

51 end_sum_loop:<br />

52 mul $t2, $t2, $t3 # set the sign properly.<br />

53<br />

54 move $a0, $t2 # print out the answer (t2).<br />

55 li $v0, 1<br />

56 syscall<br />

57<br />

58 la $a0, newline # and then print out a newline.<br />

59 li $v0, 4<br />

60 syscall<br />

61<br />

62 b exit<br />

63<br />

64 overflow: # indicate that an overflow occurred.<br />

65 la $a0, overflow_msg<br />

66 li $v0, 4<br />

67 syscall<br />

68 b exit<br />

69<br />

70 exit: # exit the program:<br />

71 li $v0, 10 # load "exit" into $v0.<br />

72 syscall # make the system call.<br />

73<br />

74 .data ## Start of data declarations:<br />

75 newline: .asciiz "\n"<br />

76 overflow_msg: .asciiz "Overflow!\n"<br />

77 string_space: .space 1024 # reserve 1024 bytes for the string.<br />

78<br />

79 ## end of atoi-4.asm

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!