I'm trying to print single character on the screen using assembly inserts with this code:
char z[]="x"; int result; asm( "mov 4, %%eax" : ); asm( "mov 1, %%ebx" : ); asm( "mov %%edx, %%ecx" : : "d"(&z) ); asm( "mov 2, %%edx" : ); //int 80h But I don't known how to insert 80h function call into this c++ code.
asm("int 80h") and asm("int 80h" : ) did not work. I am using clang++
OK, I actually solved this problem with this code which compiles succesfully:
char z[]="x"; int result; asm( "mov 4, %%eax" : : : "eax" ); asm( "mov 1, %%ebx" : : : "ebx" ); asm( "mov %%edx, %%ecx" : : "d"(&z) : "ecx" ); asm( "mov 2, %%edx" : : : "edx" ); asm( "int $0x80" : ); But after executing I received segmentation fault error. I'm also receiving this message with -fsanitize=undefined,address flag:
AddressSanitizer:DEADLYSIGNAL ================================================================= ==6068==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000004 (pc 0x558c9691bb24 bp 0x7ffde7b2e180 sp 0x7ffde7b2e0c0 T0) ==6068==The signal is caused by a READ memory access. ==6068==Hint: address points to the zero page. #0 0x558c9691bb23 (/notroot/Documents/asmcheckcpp.e+0x137b23) #1 0x7faa9eb49ce2 (/usr/lib/libc.so.6+0x23ce2) #2 0x558c9680311d (/notroot/Documents/asmcheckcpp.e+0x1f11d) AddressSanitizer can not provide additional info. SUMMARY: AddressSanitizer: SEGV (/notroot/Documents/asmcheckcpp.e+0x137b23) ==6068==ABORTING I also debugged it with gdb so I knew that this program is crushing on this line:
asm( "mov 4, %%eax" : : : "eax" ); but I do not known why. - I already solved this, so this code:
cout << 'a' << endl; char z[]="x"; asm( "mov $4, %%eax \n" "mov $1, %%ebx \n" "mov $2, %%edx \n" "int $0x80" : : "ecx"(&z) : "eax", "ebx", "edx" ); cout << 'a' << endl; works but displays
a a not
a xa on the screen
hsuffix. You have to useint $0x80. Presumably clang's built-in assembler is the same. But yeah, this code is hilariously broken. I found a duplicate that shows how to use it mostly correctly."int $0x80")mov 4, %%eaxis a load from absolute address 4. You wanted$4to have4as an immediate, not a memory address.