Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 22nd, 2008, 10:21 PM   #1
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
casting large values to small registers using ptr

Using [type size] ptr val truncates a bit range of an arguement value. [type size] = byte, word, dword, qword. This is used in intel and I believe not AT&T. My question is about around line 60. Why is there auto casting? I should have to explicitly say cast/truncate by using ptr. Is this just because I'm using Eclipse CDT IDE, linux and gcc with -masm=intel? Would some one please try running this under windows not with gcc.

C Syntax (Toggle Plain Text)
  1. /* Auther: mrynit
  2.  * Date: 4-22-05
  3.  * Class: TCSS 372
  4.  * Assign: ptroplab
  5.  *
  6.  * System developed on:
  7.  * Ubuntu 7.10
  8.  * gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)
  9.  * using flag -masm=intel for intel syntax instead of AT&T.
  10.  * Eclipse Platform Version: 3.3.1.1
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15.  
  16. /* All varieabls here are global so thay can be used in ASM. */
  17.  
  18. /* 'A' I chose this value so it would be a printable character*/
  19. char c = 65;
  20.  
  21. /* Max positive value of signed int. */
  22. int i = 2147483647;
  23. double d = 128.128;
  24.  
  25. /* holds value from the registers used for printing*/
  26. int int_holder1;
  27. int int_holder2;
  28. int int_holder3;
  29. int int_holder4;
  30.  
  31. int main(int argc, char* argv[])
  32. {
  33.  
  34. /* c is char 8 bits. moving a small value into a large or equal
  35. * register works fine
  36. */
  37. asm(
  38. ".intel_syntax noprefix;"
  39. "mov al, c;" /* 8 bit low */
  40. "mov ah, c;" /* 8 bit high */
  41. "mov ax, c;" /* 16 bit */
  42. "mov eax, c;" /* 32 bit */
  43. "mov int_holder1, al;"
  44.  
  45. /* placed the values in the registers into int variables so the
  46. * register values can be printed to screen*/
  47. "mov int_holder2, ah;"
  48. "mov int_holder3, ax;"
  49. "mov int_holder4, eax;"
  50. );
  51.  
  52. printf("the value of c = %d\n", c);
  53. printf("moved value of i placed into al = %d\n", int_holder1);
  54. printf("moved value of i placed into ah = %d\n", int_holder2);
  55. printf("moved value of i placed into ax = %d\n", int_holder3);
  56. printf("moved value of i placed into eax = %d\n\n", int_holder4);
  57.  
  58.  
  59. /* moving a large value into a smaller register SHOULD not work.
  60. * Why do these work? For some reason on my setup(see above)
  61. * "autocasting" is on I don not have to use [size typte] ptr [var]
  62. * I wonder if running this code in windows would work.
  63. */
  64. asm(
  65. "mov al, i;"
  66. "mov ah, i;"
  67. "mov ax, i;"
  68. "mov eax, i;"
  69.  
  70. "mov int_holder1, al;"
  71. "mov int_holder2, ah;"
  72. "mov int_holder3, ax;"
  73. "mov int_holder4, eax;"
  74.  
  75. );
  76.  
  77. printf("the value of i = %d\n", i);
  78. printf("moved value of i placed into al = %d\n", int_holder1);
  79. printf("moved value of i placed into ah = %d\n", int_holder2);
  80. printf("moved value of i placed into ax = %d\n", int_holder3);
  81. printf("moved value of i placed into eax = %d\n\n", int_holder4);
  82.  
  83. /*However if i want to explicitly cast large value to small I can.
  84. * I have no idea why this works.
  85. */
  86. asm(
  87. "mov al, byte ptr i;"
  88. "mov ah, byte ptr i;"
  89. "mov ax, word ptr i;"
  90. "mov eax, dword ptr i;"
  91.  
  92. "mov int_holder1, al;"
  93. "mov int_holder2, ah;"
  94. "mov int_holder3, ax;"
  95. "mov int_holder4, eax;"
  96. );
  97.  
  98. printf("the value of i = %d\n", i);
  99. printf("Using ptr for cast");
  100. printf("moved value of i placed into al = %d\n", int_holder1);
  101. printf("moved value of i placed into ah = %d\n", int_holder2);
  102. printf("moved value of i placed into ax = %d\n", int_holder3);
  103. printf("moved value of i placed into eax = %d\n\n", int_holder4);
  104.  
  105.  
  106. /* Again moving large vales into small registers is done by auto
  107. * casting.
  108. */
  109. asm(
  110. "mov al, d;"
  111. "mov ah, d;"
  112. "mov ax, d;"
  113. "mov eax, d;"
  114.  
  115. "mov int_holder1, al;"
  116. "mov int_holder2, ah;"
  117. "mov int_holder3, ax;"
  118. "mov int_holder4, eax;"
  119. );
  120.  
  121. printf("the value of d = %f\n", d);
  122. printf("moved value of d placed into al = %d\n", int_holder1);
  123. printf("moved value of d placed into ah = %d\n", int_holder2);
  124. printf("moved value of d placed into ax = %d\n", int_holder3);
  125. printf("moved value of d placed into eax = %d\n\n", int_holder4);
  126.  
  127. return 0;
  128. }

Here is my output.
the value of c = 65
moved value of i placed into al = 65
moved value of i placed into ah = 0
moved value of i placed into ax = 65
moved value of i placed into eax = 65

the value of i = 2147483647
moved value of i placed into al = 255
moved value of i placed into ah = 255
moved value of i placed into ax = 65535
moved value of i placed into eax = 2147483647

the value of i = 2147483647
Using ptr for castmoved value of i placed into al = 255
moved value of i placed into ah = 255
moved value of i placed into ax = 65535
moved value of i placed into eax = 2147483647

the value of d = 128.128000
moved value of d placed into al = 106
moved value of d placed into ah = 188
moved value of d placed into ax = 48234
moved value of d placed into eax = -1821066134
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting signed int to unsigned int? null_ptr0 C++ 1 Nov 23rd, 2007 9:27 PM
Sorting without a large array sim_maroon C 12 Nov 21st, 2005 8:45 PM
Returning two values Navid C 19 Jun 13th, 2005 2:57 AM
Problem Inserting Values into mySQL from PHP stakeknife PHP 1 Mar 23rd, 2005 8:45 AM
Program gets aborted on large input shinni C++ 3 Mar 12th, 2005 8:26 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:58 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC