Arithmetic Overflow - MIPS

Reply

Join Date: Jun 2009
Posts: 6
Reputation: pokerstar is an unknown quantity at this point 
Solved Threads: 0
pokerstar pokerstar is offline Offline
Newbie Poster

Arithmetic Overflow - MIPS

 
0
  #1
Jun 30th, 2009
I am converting an MASM procedure to MIPS.
Here's the MASM:
  1. ; --------------------------------------------------------
  2. ; Find basic list stats - minimum, median, and maximum.
  3.  
  4. ; Arguments passed:
  5. ; list, addr (4)
  6. ; length, value (6)
  7. ; minimum, addr (8)
  8. ; maximum, addr (10)
  9. ; median, addr (12)
  10.  
  11. stats1 proc near
  12.  
  13. pop bp
  14. mov bp, sp
  15.  
  16. push ax
  17. push bx
  18. push cx
  19. push dx
  20.  
  21. ; -----
  22. ; Find max.
  23.  
  24. mov bx, word ptr 4[bp]
  25. mov ax, word ptr [bx]
  26. push bx
  27. mov bx, word ptr 10[bp]
  28. mov word ptr [bx], ax
  29.  
  30. ; -----
  31. ; Find min.
  32.  
  33. mov ax, word ptr 6[bp]
  34. dec ax
  35. add ax, ax
  36. pop bx
  37.  
  38. add bx, ax
  39. mov ax, word ptr [bx]
  40. mov bx, word ptr 8[bp]
  41. mov word ptr [si], ax
  42.  
  43. ; -----
  44. ; Find median.
  45.  
  46. mov bx, word ptr 4[bp]
  47. mov cx, word ptr 6[bp]
  48. mov si, cx
  49.  
  50. mov ax, si
  51. and ax, 1
  52. cmp ax, 0
  53. je even_lp
  54.  
  55. dec si
  56. mov ax, word ptr [bx][si]
  57. jmp odd_lp
  58. ; -----
  59. ; Length is even.
  60.  
  61. even_lp:
  62. mov ax, word ptr [bx][si]
  63. sub si, 2
  64. add ax, word ptr [bx][si]
  65. mov dx, 0
  66. div wtwo
  67.  
  68. ; -----
  69. ; Length is odd.
  70.  
  71. odd_lp:
  72. mov bx, word ptr 12[bp]
  73. mov word ptr [bx], ax
  74.  
  75. pop dx
  76. pop cx
  77. pop bx
  78. pop ax
  79. pop bp
  80.  
  81. ret 10
  82.  
  83. stats1 endp

Now here's my conversion to MIPS:
  1. # -----
  2. # Arguments:
  3. # $a0 - starting address of the list
  4. # $a1 - list length
  5. # $a2 - addr of minimum
  6. # $a3 - addr of median
  7. # ($fp) - addr of maximum
  8. # 4($fp) - addr of sum
  9. # 8($fp) - addr of average
  10. .globl stats
  11. .ent stats
  12. stats:
  13.  
  14. subu $sp, $sp, 12
  15. sw $s0, 0($sp)
  16. sw $fp, 4($sp)
  17. sw $ra, 8($sp)
  18. addu $fp, $sp, 12
  19. # -----
  20. # Find min.
  21.  
  22. lw $t0, ($a0)
  23. sw $t0, ($a2) #minimum, first element of array
  24. # -----
  25. #Find max.
  26.  
  27. subu $t0, $a1, 1
  28. mul $t0, $t0, 4
  29. addu $t0, $t0, $a0
  30. lw $t1, ($t0)
  31. sw $t1, ($fp) #maximum, last element of array
  32.  
  33. # -----
  34. # Find median.
  35.  
  36. lw $t0, ($a0)
  37. move $t1, $a1
  38. andi $t2, $t1, 1
  39. beq $t2, $zero, even_lp
  40.  
  41. subu $t1, $a1, 1
  42. mul $t1, $t1, 4
  43. addu $t1, $t1, $a0
  44. lw $t2, ($t1)
  45. b odd_lp
  46.  
  47. even_lp:
  48. mul $t3, $t1, 4
  49. addu $t3, $t3, $a0
  50. lw $t4, ($t3)
  51. subu $t1, $t1, 1
  52. mul $t5, $t1, 4
  53. addu $t5, $t5, $a0
  54. lw $t6, ($t5)
  55. addu $t6, $t6, $t4
  56. div $t6, $t6, 2
  57.  
  58. odd_lp:
  59. sw $t2, ($a2) #median
  60.  
  61. sw $ra, 8($sp)
  62. sw $fp, 4($fp)
  63. sw $s0, 0($sp)
  64. addu $sp, $sp, 12
  65. jr $ra
  66.  
  67. .end stats

Now I don't know why the console displays arithmetic overflow.
What am I doing wrong?
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

Re: Arithmetic Overflow - MIPS

 
0
  #2
Jun 30th, 2009
And why are you storing your return address into your address of average?

Also in odd_lp you're storing the list entry to the minimum $a2 not the median $a3 as your comment says


	sw	$t2, ($a2)		   Save minimum   	#median

	sw	$ra, 8($sp)        Save return address?

	sw	$fp, 4($fp)
	sw	$s0, 0($sp)
	addu	$sp, $sp, 12
	jr	$ra

You know the 80x86 code has a bug?

You should have displayed the C equivalent function. Finding someone knowing both 80x86 and MIPS can be difficult.


OH, this is assembly language. Almost every line of code needs documentation. At a minimum every sub block. Detailing what data is being accessed!

On the PC you are using an array of 16-bit numbers?
And on the MIPS an array of 32-bit numbers?

If int list[] was used on both platforms then the memory access is correct. If not then a problem as the following discusses.

If the data on the MIPS is 16-bit then you need to manipulate the load. That is load 32-bit and shift the number into position (high/Low) Load, or you'll get an alignment error.

If the data on MIPS is 16-bit then you are running off the end of the list, thus bad data.

Since the 80x86 version didn't overflow I'm assuming your numbers are small values. But on the MIPS if 16-bit then you're reading pairs of numbers which appear to be a very large number thus easy to overflow. You are using lw meaning load word and a word is 32-bit.
  1. 80x86 MIPS
  2. byte byte 8-bit
  3. word half-word 16-bit
  4. dword word 32-bit
  5. qword dword 64-bit

You are scanning the list backwards which isn't memory efficient, however, you don't need to keep doing your pointer math. Merely set your pointer to your list[] entry, then either step backwards by (4 if 32-bit) until the pointer reaches the advancing pointerl. (Beginning of buffer). Or use your length count as a down count by two, and step the two pointers toward each other. When count reaches one or zero you've finish parsing the list. Better yet, pre-subtract one so when the count reaches zero or negative then you're done.

Also why are you dividing by two, when you merely need to do a logical shift right by 1.



I'll keep looking for your problem! But that should be something to double-check, as well as comment in your code!

BTW - In the 80x86 code they could have used a scalar with the register for the memory access and saved some operands.



And where is your loop for all list elements? It seems to only be looking at the last two!
Last edited by wildgoose; Jun 30th, 2009 at 2:56 pm. Reason: moving a notation!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC