assembly

Null-terminating a string in MIPS?

岁酱吖の 提交于 2021-01-05 14:43:23
问题 I'm writing strncpy in MIPS, but I'm having trouble null-terminating the string. If i do not null-terminate it myself, the string goes on and on. I have tried sb $__ 0($0) but that does not seem to work... $a0 = pointer to destination array $a1 = source string $a2 = number of characters to copy strncpy: add $t1 $zero $zero #counter beq $a2 $0 done # if num chars to copy is 0, return. j cpyLoop cpyLoop: beq $t1 $a2 done # if counter == num to copy, end lb $t2 0($a1) # load the character beq

Null-terminating a string in MIPS?

北战南征 提交于 2021-01-05 14:42:26
问题 I'm writing strncpy in MIPS, but I'm having trouble null-terminating the string. If i do not null-terminate it myself, the string goes on and on. I have tried sb $__ 0($0) but that does not seem to work... $a0 = pointer to destination array $a1 = source string $a2 = number of characters to copy strncpy: add $t1 $zero $zero #counter beq $a2 $0 done # if num chars to copy is 0, return. j cpyLoop cpyLoop: beq $t1 $a2 done # if counter == num to copy, end lb $t2 0($a1) # load the character beq

Inline assembly : register referencing conventions

倾然丶 夕夏残阳落幕 提交于 2021-01-05 12:53:47
问题 In gcc inline assembly examples found on the net, I see register names being referenced sometimes with a single % and other times with two (i.e. %% ). It's not clear when to use a single % and when to use %% . e.g. please see below example. /* Do b = a */ int a=10, b; asm ("movl %1, %%eax;\n" "movl %%eax, %0;" : "=r" (b) /* output */ : "r" (a) /* input */ : "%eax" /* clobbered register */ ); This example which uses %% prefix for EAX register compiles fine on my x86 machine (Linux RedHat 5.6

Inline assembly : register referencing conventions

时光毁灭记忆、已成空白 提交于 2021-01-05 12:53:39
问题 In gcc inline assembly examples found on the net, I see register names being referenced sometimes with a single % and other times with two (i.e. %% ). It's not clear when to use a single % and when to use %% . e.g. please see below example. /* Do b = a */ int a=10, b; asm ("movl %1, %%eax;\n" "movl %%eax, %0;" : "=r" (b) /* output */ : "r" (a) /* input */ : "%eax" /* clobbered register */ ); This example which uses %% prefix for EAX register compiles fine on my x86 machine (Linux RedHat 5.6

Image base comes out wrong in manually-generated PE headers for a 64-bit executable

谁都会走 提交于 2021-01-05 07:43:38
问题 I want to build Custom Portable executable using NASM. After getting executable using command nasm -f bin program.asm -o program.exe it crashes and gives error similar to this Below you can see the code. bits 64 %define BASE 400000h ALIGNMENT equ 512 %define SECTALIGN 8192 STD_OUTPUT_HANDLE equ -11 NULL equ 0 %define ROUND(v, a) (((v + a - 1) / a) * a) %define ALIGNED(v) (ROUND(v, ALIGNMENT)) %define RVA(obj) (obj - BASE) section header progbits start=0 vstart=BASE mz_hdr: dw "MZ" ; DOS magic

Image base comes out wrong in manually-generated PE headers for a 64-bit executable

家住魔仙堡 提交于 2021-01-05 07:40:51
问题 I want to build Custom Portable executable using NASM. After getting executable using command nasm -f bin program.asm -o program.exe it crashes and gives error similar to this Below you can see the code. bits 64 %define BASE 400000h ALIGNMENT equ 512 %define SECTALIGN 8192 STD_OUTPUT_HANDLE equ -11 NULL equ 0 %define ROUND(v, a) (((v + a - 1) / a) * a) %define ALIGNED(v) (ROUND(v, ALIGNMENT)) %define RVA(obj) (obj - BASE) section header progbits start=0 vstart=BASE mz_hdr: dw "MZ" ; DOS magic

Observing x86 register dependencies

。_饼干妹妹 提交于 2021-01-05 07:16:24
问题 Are there any other processor registers (e.g. flags) besides the architectural registers (eax, ebx,.) in x86 for which RAW dependencies need to be enforced by the scoreboard in pipelined processors? 回答1: Literally every register guarantees that if you write it, later instructions will read the new value. x86 is defined in terms of serial execution; pipelining and out-of-order exec need to preserve that illusion for everything , including segment registers, FP rounding modes, control and debug

GCC Inline-Assembly Error: “Operand size mismatch for 'int'”

丶灬走出姿态 提交于 2021-01-04 05:59:52
问题 first, if somebody knows a function of the Standard C Library, that prints a string without looking for a binary zero, but requires the number of characters to draw, please tell me! Otherwise, I have this problem: void printStringWithLength(char *str_ptr, int n_chars){ asm("mov 4, %rax");//Function number (write) asm("mov 1, %rbx");//File descriptor (stdout) asm("mov $str_ptr, %rcx"); asm("mov $n_chars, %rdx"); asm("int 0x80"); return; } GCC tells the following error to the "int" instruction:

GCC Inline-Assembly Error: “Operand size mismatch for 'int'”

萝らか妹 提交于 2021-01-04 05:59:11
问题 first, if somebody knows a function of the Standard C Library, that prints a string without looking for a binary zero, but requires the number of characters to draw, please tell me! Otherwise, I have this problem: void printStringWithLength(char *str_ptr, int n_chars){ asm("mov 4, %rax");//Function number (write) asm("mov 1, %rbx");//File descriptor (stdout) asm("mov $str_ptr, %rcx"); asm("mov $n_chars, %rdx"); asm("int 0x80"); return; } GCC tells the following error to the "int" instruction:

How does gcc know the register size to use in inline assembly?

六眼飞鱼酱① 提交于 2021-01-04 05:47:29
问题 I have the inline assembly code: #define read_msr(index, buf) asm volatile ("rdmsr" : "=d"(buf[1]), "=a"(buf[0]) : "c"(index)) The code using this macro: u32 buf[2]; read_msr(0x173, buf); I found the disassembly is(using gnu toolchain): mov eax,0x173 mov ecx,eax rdmsr mov DWORD PTR [rbp-0xc],edx mov DWORD PTR [rbp-0x10],eax The question is that 0x173 is less than 0xffff, why gcc does not use "mov cx, 0x173"? Will the gcc analysis the following instruction "rdmsr"? Will the gcc always know the