passing command line parameters mips

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 31
Reputation: shahab03 is an unknown quantity at this point 
Solved Threads: 0
shahab03 shahab03 is offline Offline
Light Poster

passing command line parameters mips

 
0
  #1
Oct 28th, 2009
I have written a program that multiplies 2 decimals. My multiplication output is fine.

However I would like to do multiplication by passing in two parameters via command line. So far e.g. lets say if I just put '3 4' in the command line without the quotes.. nothing happens...

however if I put the numbers like this 'mult 3 4' -- of course without quote.. its works.. I see 12 on the console... is there anyway where I dont have to type in mult and just type '3 '4' ???

ideas???
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark
 
0
  #2
Oct 28th, 2009
code?

Are you trapping for a keyword mult?

How about trapping for operators

3 * 4
12
1 + 2
3
etc.

Or even
* 3 4
12
Last edited by wildgoose; Oct 28th, 2009 at 11:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: shahab03 is an unknown quantity at this point 
Solved Threads: 0
shahab03 shahab03 is offline Offline
Light Poster
 
0
  #3
Oct 28th, 2009
examples always help. thanks
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: shahab03 is an unknown quantity at this point 
Solved Threads: 0
shahab03 shahab03 is offline Offline
Light Poster
 
0
  #4
Oct 28th, 2009
meaning i can use an example..
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark
 
0
  #5
Oct 28th, 2009
Can't do that, you don't have code!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: shahab03 is an unknown quantity at this point 
Solved Threads: 0
shahab03 shahab03 is offline Offline
Light Poster
 
0
  #6
Oct 29th, 2009
here is the code...

  1. .data
  2.  
  3. errmsg:
  4. .asciiz "Two command line arguments required.\n"
  5. newline:
  6. .asciiz "\n"
  7. result:
  8. .space 11
  9.  
  10. .text
  11. .globl main
  12.  
  13. main:
  14. move $s0, $a0
  15. move $s1, $a1
  16. move $s2, $zero
  17. move $s3, $zero
  18. move $s4, $zero
  19.  
  20. # check if two arguments are given
  21. li $t0, 2
  22. ble $a0, $t0, error
  23.  
  24. # parse the first number
  25. lw $a0, 4($s1)
  26. jal atoi
  27. move $s2, $v0
  28.  
  29. # parse the second number
  30. lw $a0, 8($s1)
  31. jal atoi
  32. move $s3, $v0
  33.  
  34. # multiply the numbers
  35. mult $s2, $s3
  36. mflo $s4
  37.  
  38. # format the result
  39. la $a0, result
  40. move $a1, $s4
  41. jal itoa
  42.  
  43. # print the result
  44. li $v0, 4
  45. la $a0, result
  46. syscall
  47. li $v0, 4
  48. la $a0, newline
  49. syscall
  50.  
  51. b exit
  52. error:
  53. # print error message
  54. li $v0, 4
  55. la $a0, errmsg
  56. syscall
  57. exit:
  58. li $v0, 10
  59. li $a0, 0
  60. syscall
  61.  
  62. atoi:
  63. move $v0, $zero
  64.  
  65. # detect sign
  66. li $t0, 1
  67. lbu $t1, 0($a0)
  68. bne $t1, 45, digit
  69. li $t0, -1
  70. addu $a0, $a0, 1
  71. digit:
  72. # read character
  73. lbu $t1, 0($a0)
  74.  
  75. # finish when non-digit encountered
  76. bltu $t1, 48, finish
  77. bgtu $t1, 57, finish
  78.  
  79. # translate character into digit
  80. subu $t1, $t1, 48
  81.  
  82. # multiply the accumulator by ten
  83. li $t2, 10
  84. mult $v0, $t2
  85. mflo $v0
  86.  
  87. # add digit to the accumulator
  88. add $v0, $v0, $t1
  89.  
  90. # next character
  91. addu $a0, $a0, 1
  92. b digit
  93. finish:
  94. mult $v0, $t0
  95. mflo $v0
  96. jr $ra
  97.  
  98. itoa:
  99. subu $sp, $sp, 40
  100. sd $s0, 0($sp)
  101. sd $s2, 8($sp)
  102. sd $s4, 16($sp)
  103. sd $s6, 24($sp)
  104. sw $ra, 32($sp)
  105.  
  106. move $s0, $a0
  107. move $s1, $a1
  108.  
  109. # print minus if the number is negative
  110. bgez $s1, char
  111. li $t0, 45
  112. sb $t0, 0($s0)
  113. addu $s0, $s0, 1
  114. addu $a0, $a0, 1
  115.  
  116. # make the number positive
  117. li $t0, -1
  118. mult $s1, $t0
  119. mflo $s1
  120. char:
  121. # divide number by ten
  122. li $t0, 10
  123. divu $s1, $t0
  124. mflo $s1
  125. mfhi $t0
  126.  
  127. # translate digit to character
  128. addu $t0, $t0, 48
  129.  
  130. # write character
  131. sb $t0, 0($a0)
  132.  
  133. # finish if quotient is zero
  134. beqz $s1, return
  135.  
  136. # move to the next position
  137. addu $a0, $a0, 1
  138. b char
  139. return:
  140. # put null character to the end of the string
  141. move $t0, $zero
  142. sb $t0, 1($a0)
  143.  
  144. # reverse the string with the digits
  145. move $a0, $s0
  146. jal reverse
  147.  
  148. ld $s0, 0($sp)
  149. ld $s2, 8($sp)
  150. ld $s4, 16($sp)
  151. ld $s6, 24($sp)
  152. lw $ra, 32($sp)
  153. addu $sp, $sp, 40
  154. jr $ra
  155.  
  156. reverse:
  157. # find the last character in the string
  158. move $t0, $a0
  159. next:
  160. lbu $t1, 0($t0)
  161. beqz $t1, last
  162. addu $t0, $t0, 1
  163. b next
  164. last:
  165. # move to the last character
  166. subu $t0, $t0, 1
  167. swap:
  168. # done when pointers meet
  169. bgeu $a0, $t0, done
  170.  
  171. # exchange characters
  172. lbu $t2, 0($a0)
  173. lbu $t3, 0($t0)
  174. sb $t2, 0($t0)
  175. sb $t3, 0($a0)
  176.  
  177. # step to the next pair
  178. addu $a0, $a0, 1
  179. subu $t0, $t0, 1
  180. b swap
  181. done:
  182. jr $ra
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 31
Reputation: shahab03 is an unknown quantity at this point 
Solved Threads: 0
shahab03 shahab03 is offline Offline
Light Poster
 
0
  #7
Oct 29th, 2009
here is the code...

  1. .data
  2.  
  3. errmsg:
  4. .asciiz "Two command line arguments required.\n"
  5. newline:
  6. .asciiz "\n"
  7. result:
  8. .space 11
  9.  
  10. .text
  11. .globl main
  12.  
  13. main:
  14. move $s0, $a0
  15. move $s1, $a1
  16. move $s2, $zero
  17. move $s3, $zero
  18. move $s4, $zero
  19.  
  20. # check if two arguments are given
  21. li $t0, 2
  22. ble $a0, $t0, error
  23.  
  24. # parse the first number
  25. lw $a0, 4($s1)
  26. jal atoi
  27. move $s2, $v0
  28.  
  29. # parse the second number
  30. lw $a0, 8($s1)
  31. jal atoi
  32. move $s3, $v0
  33.  
  34. # multiply the numbers
  35. mult $s2, $s3
  36. mflo $s4
  37.  
  38. # format the result
  39. la $a0, result
  40. move $a1, $s4
  41. jal itoa
  42.  
  43. # print the result
  44. li $v0, 4
  45. la $a0, result
  46. syscall
  47. li $v0, 4
  48. la $a0, newline
  49. syscall
  50.  
  51. b exit
  52. error:
  53. # print error message
  54. li $v0, 4
  55. la $a0, errmsg
  56. syscall
  57. exit:
  58. li $v0, 10
  59. li $a0, 0
  60. syscall
  61.  
  62. atoi:
  63. move $v0, $zero
  64.  
  65. # detect sign
  66. li $t0, 1
  67. lbu $t1, 0($a0)
  68. bne $t1, 45, digit
  69. li $t0, -1
  70. addu $a0, $a0, 1
  71. digit:
  72. # read character
  73. lbu $t1, 0($a0)
  74.  
  75. # finish when non-digit encountered
  76. bltu $t1, 48, finish
  77. bgtu $t1, 57, finish
  78.  
  79. # translate character into digit
  80. subu $t1, $t1, 48
  81.  
  82. # multiply the accumulator by ten
  83. li $t2, 10
  84. mult $v0, $t2
  85. mflo $v0
  86.  
  87. # add digit to the accumulator
  88. add $v0, $v0, $t1
  89.  
  90. # next character
  91. addu $a0, $a0, 1
  92. b digit
  93. finish:
  94. mult $v0, $t0
  95. mflo $v0
  96. jr $ra
  97.  
  98. itoa:
  99. subu $sp, $sp, 40
  100. sd $s0, 0($sp)
  101. sd $s2, 8($sp)
  102. sd $s4, 16($sp)
  103. sd $s6, 24($sp)
  104. sw $ra, 32($sp)
  105.  
  106. move $s0, $a0
  107. move $s1, $a1
  108.  
  109. # print minus if the number is negative
  110. bgez $s1, char
  111. li $t0, 45
  112. sb $t0, 0($s0)
  113. addu $s0, $s0, 1
  114. addu $a0, $a0, 1
  115.  
  116. # make the number positive
  117. li $t0, -1
  118. mult $s1, $t0
  119. mflo $s1
  120. char:
  121. # divide number by ten
  122. li $t0, 10
  123. divu $s1, $t0
  124. mflo $s1
  125. mfhi $t0
  126.  
  127. # translate digit to character
  128. addu $t0, $t0, 48
  129.  
  130. # write character
  131. sb $t0, 0($a0)
  132.  
  133. # finish if quotient is zero
  134. beqz $s1, return
  135.  
  136. # move to the next position
  137. addu $a0, $a0, 1
  138. b char
  139. return:
  140. # put null character to the end of the string
  141. move $t0, $zero
  142. sb $t0, 1($a0)
  143.  
  144. # reverse the string with the digits
  145. move $a0, $s0
  146. jal reverse
  147.  
  148. ld $s0, 0($sp)
  149. ld $s2, 8($sp)
  150. ld $s4, 16($sp)
  151. ld $s6, 24($sp)
  152. lw $ra, 32($sp)
  153. addu $sp, $sp, 40
  154. jr $ra
  155.  
  156. reverse:
  157. # find the last character in the string
  158. move $t0, $a0
  159. next:
  160. lbu $t1, 0($t0)
  161. beqz $t1, last
  162. addu $t0, $t0, 1
  163. b next
  164. last:
  165. # move to the last character
  166. subu $t0, $t0, 1
  167. swap:
  168. # done when pointers meet
  169. bgeu $a0, $t0, done
  170.  
  171. # exchange characters
  172. lbu $t2, 0($a0)
  173. lbu $t3, 0($t0)
  174. sb $t2, 0($t0)
  175. sb $t3, 0($a0)
  176.  
  177. # step to the next pair
  178. addu $a0, $a0, 1
  179. subu $t0, $t0, 1
  180. b swap
  181. done:
  182. jr $ra
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark
 
0
  #8
Oct 29th, 2009
Command line Arguments are typically passed in one of two ways to a program at startup.

int count count of arguments
char *argv[] array of arguments

This seems to be the way you're handling it, and your code seems okay.
An alternate method is a single string where arguments have to be parsed but you aren't setup that way. You can be because your AtoI function tracks where the number ended and a non-digit continues.

This is assembly code, I am recommending much more commenting then you are doing!
For example:
  1. # _________________
  2. # ASCIIz to signed integer
  3. #
  4. # (int) v0 = atoi( char * a0 )
  5.  
  6. atoi:
  7. move $v0, $zero
  8.  
  9. # detect sign
  10. li $t0, 1
  11. lbu $t1, 0($a0)
  12. bne $t1, 45, digit # (C <> '-')
  13.  
  14. # NEGATIVE
  15. li $t0, -1
  16. addu $a0, $a0, 1
  17.  
  18. digit:
  19. # read character
  20. lbu $t1, 0($a0)
  21.  
  22. # finish when non-digit encountered
  23. bltu $t1, 48, finish # (C < '0')
  24. bgtu $t1, 57, finish # (C > '9')
  25.  
  26. # translate character into digit
  27. subu $t1, $t1, 48 # C -= '0'
  28.  
  29. # multiply the accumulator by ten
  30. li $t2, 10
  31. mult $v0, $t2
  32. mflo $v0 # v0 = Lo result
  33.  
  34. # add digit to the accumulator
  35. add $v0, $v0, $t1 # N = (N * 10) + A
  36.  
  37. # next character
  38. addu $a0, $a0, 1 # advance pointer
  39. b digit
  40.  
  41. # handle sign
  42. finish:
  43. mult $v0, $t0 # N *= { 1 : -1 }
  44. mflo $v0
  45. jr $ra
Last edited by wildgoose; Oct 29th, 2009 at 2:41 pm.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for Assembly
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC