Vulpes

float_asm_discussion

Nov 30th, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.94 KB | None | 0 0
  1. [12:27] • Vulpes raises hand:
  2. [12:27] Vulpes: if I empty off my floating register-stack, will it affect other variables that I have set outside my assembly code?
  3. [12:29] Vulpes: ie, if I: main(){ double a=1.0f; assembly_call_erases_fpstack(); printf("%f \n",a); etc...
  4. [12:29] Vulpes: in my code, a is now NAN. was the float value never saved to memory?
  5. [12:30] Jester01: maybe. check the assembly code. and the calling convention documentation :)
  6. [12:33] Jester01: generally you should only pop values you pushed
  7. [12:34] iiyo ([email protected]) joined the chat room.
  8. [12:36] Jester01: for the record, my gcc moves the variable initialization to after the assembly call
  9. [12:37] • Jester01 tries passing "a" as argument
  10. [12:37] Vulpes: I can link you my code, but I'm not sure you want to get your hands this dirty
  11. [12:38] Jester01: sure :)
  12. [12:39] arek_deepinit ([email protected]) joined the chat room.
  13. [12:39] • Vulpes looks at his .c
  14. [12:39] zach (817442c2@gateway/web/freenode/ip.129.116.66.194) joined the chat room.
  15. [12:39] Vulpes: wait ... hold up
  16. [12:39] AlbertoSilva ([email protected]) left the chat room. (Read error: Connection reset by peer)
  17. [12:39] Jester01: no matter what I did so far, gcc always called other functions with empty fpu stack
  18. [12:40] AlbertoSilva ([email protected]) joined the chat room.
  19. [12:42] monsterwizard ([email protected]) left the chat room. (Read error: Connection reset by peer)
  20. [12:42] FUZxxl ([email protected]) left the chat room. (Quit: euler=:[:+/i.*[:+./0=3 5|"0 1 i.)
  21. [12:42] zach: i have something strange going on when I assemble something with nasm. The line verbatim is "BootDrive equ bp-2" but it gives the error "invalid operand type." Help?
  22. [12:44] Jester01: that's invalid because EQU is trying to define a symbol with a known value. your value is dependent on value of the BP register at runtime. if you want simple text replacement, use a macro
  23. [12:44] Jester01: such as %define BootDrive bp-2
  24. [12:47] zach: ah, makes sense, thanks
  25. [12:47] Philopon ([email protected]) joined the chat room.
  26. [12:48] Vulpes: links:
  27. [12:48] Vulpes: http://pastebin.com/5q00PFs6 http://pastebin.com/BC8tmwx1
  28. [12:48] Vulpes: not necessary to view: http://pastebin.com/5NGJHAvJ http://pastebin.com/wc9JvVLr
  29. [12:49] Vulpes: so all I'm doing is loading all the floats up into the stack so I can print them out, which works
  30. [12:49] Vulpes: then I attempt to load them all back in
  31. [12:49] wobster ([email protected]) left the chat room. (Quit: wobster)
  32. [12:49] Vulpes: then I'm done ... or so I was to think so
  33. [12:51] Vulpes: on that 4th link, lines 144-151 print out the floats on the stack
  34. [12:51] Jester01: your asm code is wrong, but should not affect the C code
  35. [12:51] Vulpes: which were put in there on 2nd link lines 36-45
  36. [12:52] Vulpes: which places?
  37. [12:55] Vulpes: 37 & 39 are supposed to be dwords, right? not qwords? I'd assume so because they print out correctly
  38. [12:55] repozitor ([email protected]) left the chat room.
  39. [12:56] Jester01: you are popping 8 values from the stack, even if there were not that many present
  40. [12:56] Jester01: then you push them back
  41. [12:56] Jester01: so you will always return with a full stack
  42. [12:57] Vulpes: is there something wrong with that?
  43. [12:57] Jester01: furthermore, you are adjusting the stack pointer every time, so what is line 173 doing?
  44. [12:57] Jester01: YES
  45. [12:57] Jester01: as I said, you are not restoring the fpu stack
  46. [12:58] Jester01: you always pop off 8 values, even if there were not that many
  47. [12:58] Vulpes: oh sorry 173 sould be commented out
  48. [12:58] Jester01: and you always push 8 back
  49. [12:58] Jester01: suppose stack only had 1 value
  50. [12:58] Jester01: you pop 8 times
  51. [12:58] Jester01: then push back 8 items
  52. [12:58] Jester01: so now stack has 8 items instead of 1
  53. [12:59] Jester01: your test code does fill it with 8 values
  54. [12:59] Jester01: so it will work in that case
  55. [12:59] Vulpes: is there some sort of counter index that shows how many items are on the stack?
  56. [12:59] Burlingk ([email protected]) left the chat room. (Read error: Connection reset by peer)
  57. [12:59] Vulpes: I thought if you pushed too much it would fall off or popped too much, you would get garbage values, but nothing else
  58. [13:00] Vulpes: I figured if I popped everything off the float stack & then pushed it all back on, regardless if I took garbage numbers, all the data would be the same
  59. [13:00] Vulpes: am I not understanding instructions correctly?
  60. [13:02] Reversy ([email protected]) left the chat room.
  61. [13:03] plusk ([email protected]) joined the chat room.
  62. [13:05] nANdy (3ec730fd@gateway/web/freenode/ip.62.199.48.253) joined the chat room.
  63. [13:06] Jester01: hm you may be right about that (except for the stack overflow detection)
  64. [13:06] Jester01: stack top is stored in the fpu status register, btw.
  65. [13:07] • Vulpes learned something new
  66. [13:08] Jester01: HOWEVER .. FPU stack may be irrelevant to the current problem .. since you are not saving general purpose registers that you should definitely preserve
  67. [13:09] killown (~geek@unaffiliated/killown) joined the chat room.
  68. [13:09] Jester01: notably, ESI
  69. [13:10] Vulpes: ah, is my problem that my entire asm function isn't callee saving?
  70. [13:10] Vulpes: bbrb trying out
  71. [13:12] Jester01: seems to work here
  72. [13:13] Vulpes: eh? could I trouble you for your code?
  73. [13:13] Jester01: I just added push/pop esi to your test_floats and removed the unnecessary esp adjust
  74. [13:13] Jester01: note that the value of "c" is not preserved
  75. [13:14] Vulpes: that is fine.
  76. [13:14] Jester01: yah
  77. [13:14] Jester01: oh, and I have renamed your custom .txt to .text ;)
  78. [13:14] Jester01: which is probably irrelevant
  79. [13:15] Vulpes: I'm fuzzy on those pseudo ops, (not sure which fork to eat salad with)
  80. [13:15] Jester01: http://pastebin.com/uhgWMX9C
  81. [13:18] Vulpes: hmm
  82. [13:18] Vulpes: this is an interesting scenario
  83. [13:19] Jester01: I have checked, gcc here DID expect ESI to be unchanged
  84. [13:20] Vulpes: I wonder if it is the fact that I am running a VM
  85. [13:21] Vulpes: http://i.imgur.com/k60e9.png
  86. [13:22] Jester01: what happened with the code I pasted?
  87. [13:22] Jester01: crash? bad number?
  88. [13:22] Vulpes: xBFC82F38: -NAN
  89. [13:22] Vulpes: xBFC82F3C: -NAN
  90. [13:22] Vulpes: same rewrites to variables a & b
  91. [13:23] Jester01: http://pastebin.com/yHDdRQH4
  92.  
  93. [13:24] Vulpes: thanks for that output paste, I am going to see if I can dissect that, brb
  94. [13:25] zach (817442c2@gateway/web/freenode/ip.129.116.66.194) left the chat room. (Quit: Page closed)
  95. [13:39] Jester01: or you can try this binary http://filesmelt.com/dl/a.out_.gz
  96. [13:39] Jester01: (static)
  97. [13:45] rendar ([email protected]) left the chat room.
  98. [13:46] Horgh^ ([email protected]) left the chat room. (Quit: ++)
  99. [13:49] Vulpes: thanks Jester01 your binary is perfect
  100. [13:50] Vulpes: I wonder if it's my arguments
  101. [13:50] Hack004 ([email protected]) left the chat room. (Ping timeout: 252 seconds)
  102. [13:50] Jester01: can you send me your binary?
  103. [13:50] Vulpes: ok hold up
  104. [13:50] Jester01: also, you can put a write watchpoint on the address of a or b in gdb
  105. [13:50] lane ([email protected]) joined the chat room.
  106. [13:50] Jester01: to catch who is overwriting it :)
  107. [13:53] Vulpes: http://filesmelt.com/dl/div_test.out_.tar.gz
  108. [13:55] Jester01: hmmm
  109. [13:55] Vulpes: you get the NANs?
  110. [13:55] Jester01: that only prints the registers for me
  111. [13:55] Jester01: no
  112. [13:55] Jester01: I only get a single general purpose register dump :-O
  113. [13:56] Vulpes: it doesn't even print the stack?
  114. [13:56] Vulpes: did it segfault?
  115. [13:56] Jester01: no
  116. [13:56] Jester01: and no
  117. [13:56] Vulpes: http://pastebin.com/sguEB2Tn
  118. [13:56] Jester01: it doesn't even print the *initial* C stuff about a,b,c
  119. [13:57] slidercrank ([email protected]) left the chat room. (Quit: NO CARRIER)
  120. [13:57] slidercrank ([email protected]) joined the chat room.
  121. [13:57] Jester01: http://pastebin.com/Zs67gJnQ
  122. [13:57] Jester01: that's all
  123. [13:58] Jester01: can you create a static version? just pass -static to gcc
  124. [13:58] Vulpes: 1 & 3 loaded in ecx & edx ... where they were supposed to have been loaded with ebp & esp before the subroutine call
  125. [13:58] Vulpes: I don't even.
  126. [13:59] Griwes (~griwes@unaffiliated/griwes) left the chat room. (Ping timeout: 244 seconds)
  127. [14:01] Philopon ([email protected]) left the chat room. (Quit: Philopon)
  128. [14:03] plusk ([email protected]) left the chat room. (Quit: Leaving)
  129. [14:07] lane ([email protected]) left the chat room. (Ping timeout: 252 seconds)
  130. [14:08] arek_deepinit ([email protected]) left the chat room. (Read error: Connection reset by peer)
  131. [14:09] Jester01: hm, valgrind has trouble with "enter"
  132. [14:09] • Jester01 gets rid of it ... the enter, not valgrind :)
  133. [14:10] LunaVorax ([email protected]) joined the chat room.
  134. [14:10] Vulpes: hmm, maybe I really should forsake "enter" & go with the push ebp ; mov ebp esp
  135. [14:11] Jester01: ==22766== Use of uninitialised value of size 4
  136. [14:11] Jester01: ==22766== Conditional jump or move depends on uninitialised value(s)
  137. [14:11] Jester01: both inside printf
  138. [14:11] Vulpes: oh you know what
  139. [14:12] Vulpes: I disregard scope when printing out the stack
  140. [14:12] Jester01: ah yes
  141. [14:12] Jester01: that looks harmless
  142. [14:12] Jester01: and no other errors reported
  143. [14:12] Reversy ([email protected]) joined the chat room.
  144. [14:12] Smartnow (~Smartnow@unaffiliated/smartnow) joined the chat room.
  145. [14:12] Reversy ([email protected]) left the chat room.
  146. [14:12] Jester01: so, do you create a static version for me?
  147. [14:13] Jester01: maybe libc differences make your version not run here
  148. [14:13] Jester01: but first get rid of the "enter" instructions :)
  149. [14:14] SunnyD ([email protected]) joined the chat room.
  150. [14:15] Vulpes: I'm a bit unsavvy with the term. is that an assembly argument?
  151. [14:15] Vulpes: assembler*
  152. [14:15] Vulpes: also I made up that "unsavvy" word & realized it is not in the dictionary
  153. [14:19] Vulpes: I presume: gcc -static-libgcc
  154. [14:21] ImTheBitch ([email protected]) joined the chat room.
  155. [14:22] Jester01: no, just -static
  156. [14:22] jadugar (~jadugar@gateway/tor-sasl/jadugar) left the chat room. (Quit: leaving)
  157. [14:24] Jokke (6d5af7d9@gateway/web/freenode/ip.109.90.247.217) left the chat room. (Ping timeout: 265 seconds)
  158. [14:25] Vulpes: http://filesmelt.com/dl/div_test.out_.tar1.gz
  159. [14:25] Jester01: ahhhhha! got the nans :)
  160. [14:26] BoarderX ([email protected]) left the chat room. (Ping timeout: 260 seconds)
  161. [14:26] Vulpes: heh, maybe I should subtract biinfiles from eachother & see what the difference is
  162. [14:27] Jester01: I got the nan here locally
  163. [14:27] Jester01: turns out it only happens if you are NOT optimizing
  164. [14:27] neuro_sys (~neuro@unaffiliated/neurosys/x-283974) joined the chat room.
  165. [14:28] Vulpes: if I'm not optimizing, I'll get a NAN written to my float variables in the .c ?
  166. [14:28] Jester01: strangely enough they are all right in memory
  167. [14:34] Jester01: mmh, you get FPU stack overflow
  168. [14:37] Fabba (unknown@unaffiliated/fabba) joined the chat room.
  169. [14:37] ThFabba (unknown@unaffiliated/fabba) left the chat room. (Disconnected by services)
  170. [14:37] Fabba is now known as ThFabba.
  171. [14:37] Vulpes: hmm, ok.
  172. [14:37] Vulpes: so stackoverflow occurs when I'm just pumping the 1s & 0s into the stack, right?
  173. [14:38] kramaus ([email protected]) joined the chat room.
  174. [14:38] monsterwizard ([email protected]) joined the chat room.
  175. [14:38] Jester01: oh, you fill the fpu stack with 8 numbers, then during printing you pop and push them back
  176. [14:38] nazriel ([email protected]) joined the chat room.
  177. [14:38] Jester01: so they are LEFT on the stack
  178. [14:39] Vulpes: therefore ... I think I'll try unloading the fpu stack first, but by how much? how do I access the fpu status register?
  179. [14:39] Jester01: nonono
  180. [14:39] Jester01: in this case you DO know you have put 8 values on the stack
  181. [14:39] Jester01: but you LEAVE them there!
  182. [14:39] Vulpes: yes
  183. [14:39] Jester01: that's bad.
  184. [14:39] Vulpes: I'm supposed to clear it all out save st0?
  185. [14:39] Jester01: test_floats puts the values there, it should also remove them
  186. [14:39] • Jester01 tries
  187. [14:40] Jester01: erm
  188. [14:40] Jester01: except for 1
  189. [14:40] Jester01: the function return value ;)
  190. [14:40] Jester01: yay that worked
  191. [14:41] • Vulpes gets excited
  192. [14:41] Jester01: I added 7 fstp st0 lines to the end of test_floats
  193. [14:42] Jester01: nevertheless, your print_fpstack should correctly handle the stack size
  194. [14:42] Vulpes: YES!!
  195. [14:42] Vulpes: thank you so much
Advertisement
Add Comment
Please, Sign In to add comment