|
Hobbyist Programmer
Join Date: Mar 2006
Location: olympia,WA
Posts: 329
Rep Power: 3 
|
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.
/* Auther: mrynit * Date: 4-22-05 * Class: TCSS 372 * Assign: ptroplab * * System developed on: * Ubuntu 7.10 * gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2) * using flag -masm=intel for intel syntax instead of AT&T. * Eclipse Platform Version: 3.3.1.1 * */ #include <stdio.h> /* All varieabls here are global so thay can be used in ASM. */ /* 'A' I chose this value so it would be a printable character*/ char c = 65; /* Max positive value of signed int. */ int i = 2147483647; double d = 128.128; /* holds value from the registers used for printing*/ int int_holder1; int int_holder2; int int_holder3; int int_holder4; int main(int argc, char* argv[]) { /* c is char 8 bits. moving a small value into a large or equal * register works fine */ asm( ".intel_syntax noprefix;" "mov al, c;" /* 8 bit low */ "mov ah, c;" /* 8 bit high */ "mov ax, c;" /* 16 bit */ "mov eax, c;" /* 32 bit */ "mov int_holder1, al;" /* placed the values in the registers into int variables so the * register values can be printed to screen*/ "mov int_holder2, ah;" "mov int_holder3, ax;" "mov int_holder4, eax;" ); printf("the value of c = %d\n", c); printf("moved value of i placed into al = %d\n", int_holder1); printf("moved value of i placed into ah = %d\n", int_holder2); printf("moved value of i placed into ax = %d\n", int_holder3); printf("moved value of i placed into eax = %d\n\n", int_holder4); /* moving a large value into a smaller register SHOULD not work. * Why do these work? For some reason on my setup(see above) * "autocasting" is on I don not have to use [size typte] ptr [var] * I wonder if running this code in windows would work. */ asm( "mov al, i;" "mov ah, i;" "mov ax, i;" "mov eax, i;" "mov int_holder1, al;" "mov int_holder2, ah;" "mov int_holder3, ax;" "mov int_holder4, eax;" ); printf("the value of i = %d\n", i); printf("moved value of i placed into al = %d\n", int_holder1); printf("moved value of i placed into ah = %d\n", int_holder2); printf("moved value of i placed into ax = %d\n", int_holder3); printf("moved value of i placed into eax = %d\n\n", int_holder4); /*However if i want to explicitly cast large value to small I can. * I have no idea why this works. */ asm( "mov al, byte ptr i;" "mov ah, byte ptr i;" "mov ax, word ptr i;" "mov eax, dword ptr i;" "mov int_holder1, al;" "mov int_holder2, ah;" "mov int_holder3, ax;" "mov int_holder4, eax;" ); printf("the value of i = %d\n", i); printf("Using ptr for cast"); printf("moved value of i placed into al = %d\n", int_holder1); printf("moved value of i placed into ah = %d\n", int_holder2); printf("moved value of i placed into ax = %d\n", int_holder3); printf("moved value of i placed into eax = %d\n\n", int_holder4); /* Again moving large vales into small registers is done by auto * casting. */ asm( "mov al, d;" "mov ah, d;" "mov ax, d;" "mov eax, d;" "mov int_holder1, al;" "mov int_holder2, ah;" "mov int_holder3, ax;" "mov int_holder4, eax;" ); printf("the value of d = %f\n", d); printf("moved value of d placed into al = %d\n", int_holder1); printf("moved value of d placed into ah = %d\n", int_holder2); printf("moved value of d placed into ax = %d\n", int_holder3); printf("moved value of d placed into eax = %d\n\n", int_holder4); return 0; }
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
|