assembly

Why does this REPNE SCASB implementation of strlen work?

房东的猫 提交于 2020-05-09 07:41:06
问题 Why does this code work? http://www.int80h.org/strlen/ says that the string address has to be in EDI register for scasb to work, but this assembly function doesn't seem to do this. Assembly code for mystrlen : global mystrlen mystrlen: sub ecx, ecx not ecx sub al, al cld repne scasb neg ecx dec ecx dec ecx mov eax, ecx ret C main: int mystrlen(const char *); int main() { return (mystrlen("1234")); } Compilation: nasm -f elf64 test.asm gcc -c main.c gcc main.o test.o Output: ./a.out echo $? 4

Creating an x86 assembler program that converts an integer to a 16-bit binary string of 0's and 1's

核能气质少年 提交于 2020-05-09 06:57:46
问题 As the question suggests, I have to write a MASM program to convert an integer to binary. I have tried many different approaches, but none of them helped me at all. The final code I'm working on is as follows. I get an access memory violation error when I debug my code in Visual Studio . Any help on how to solve the error and if I'm on the right track or not will be greatly appreciated. The first code is my C++ code which passes a char array to an .asm file to be converted to binary. #include

Regarding cmp / jg, jle, etc in AT&T syntax assembly

↘锁芯ラ 提交于 2020-05-09 06:36:05
问题 So every single resource online tells me that something like this: cmp %eax, %ebx jg < something > would jump to < something > if eax was greater than ebx. But I have another piece of code that seems to contradict this: cmp $0x2, %eax jg < something> as it jumps to < something > when eax has the value 3. Am I missing something, or does cmp a, b - jg execute if b > a and not a>b? And does this apply to other jump statements as well? 回答1: When we read something like cmp $0x2, %eax jg <

Why does gcc generates strange code without flag -fno-pie?

眉间皱痕 提交于 2020-05-09 06:35:09
问题 I am trying to compile dummy function in gcc with flag -fno-pie and without. void dummy_test_entrypoint() { } When i compile without the flag. gcc -m32 -ffreestanding -c test.c -o test.o I get the following disassembled code. 00000000 <dummy_test_entrypoint>: 0: 55 push ebp 1: 89 e5 mov ebp,esp 3: e8 fc ff ff ff call 4 <dummy_test_entrypoint+0x4> 8: 05 01 00 00 00 add eax,0x1 d: 90 nop e: 5d pop ebp f: c3 ret When i compile with the flag. 00000000 <dummy_test_entrypoint>: 0: 55 push ebp 1: 89

Why does gcc generates strange code without flag -fno-pie?

蹲街弑〆低调 提交于 2020-05-09 06:35:01
问题 I am trying to compile dummy function in gcc with flag -fno-pie and without. void dummy_test_entrypoint() { } When i compile without the flag. gcc -m32 -ffreestanding -c test.c -o test.o I get the following disassembled code. 00000000 <dummy_test_entrypoint>: 0: 55 push ebp 1: 89 e5 mov ebp,esp 3: e8 fc ff ff ff call 4 <dummy_test_entrypoint+0x4> 8: 05 01 00 00 00 add eax,0x1 d: 90 nop e: 5d pop ebp f: c3 ret When i compile with the flag. 00000000 <dummy_test_entrypoint>: 0: 55 push ebp 1: 89

80286: Which is the fastest way to multiply by 10?

浪子不回头ぞ 提交于 2020-05-09 06:31:07
问题 To multiply a number by any any multiple of 2, I'll shift it those many times. Is there any such technique to multiply a number by 10 in less cycles? 回答1: The 80286 did not have a barrel shifter, that was introduced with the 80386. According to the timing tables in the Microsoft Macro Assembler 5.0 documentation (1987), SHL reg, immed8 takes 5+n cycles, whereas SHL reg, 1 takes 2 cycles. ADD reg, reg takes 2 cycles, as does MOV reg, reg . IMUL reg16, immed takes 21 cycles. Therefore, the

80286: Which is the fastest way to multiply by 10?

荒凉一梦 提交于 2020-05-09 06:31:05
问题 To multiply a number by any any multiple of 2, I'll shift it those many times. Is there any such technique to multiply a number by 10 in less cycles? 回答1: The 80286 did not have a barrel shifter, that was introduced with the 80386. According to the timing tables in the Microsoft Macro Assembler 5.0 documentation (1987), SHL reg, immed8 takes 5+n cycles, whereas SHL reg, 1 takes 2 cycles. ADD reg, reg takes 2 cycles, as does MOV reg, reg . IMUL reg16, immed takes 21 cycles. Therefore, the

How to properly setup SS, BP and SP in x86 Real Mode?

泄露秘密 提交于 2020-05-09 05:11:48
问题 I want to know how to properly do it, because the way I'm doing it isn't working. When setting the BP register with 7C00h, then setting the SP register with BP , then pushing some ASCII, then getting the data from the memory to print it with INT 10h , it works just fine. mov ax, 7C00h mov bp, ax mov sp, bp push 'A' mov ah, 0Eh mov al, [7BFEh] int 10h The actual output is A But when I do this: mov ax, 7C00h mov ss, ax mov bp, ax mov sp, bp ... It stops working. The interrupt is called, the

Does Skylake need vzeroupper for turbo clocks to recover after a 512-bit instruction that only reads a ZMM register, writing a k mask?

狂风中的少年 提交于 2020-05-09 02:27:56
问题 Writing a ZMM register can leave a Skylake-X (or similar) CPU in a state of reduced max-turbo indefinitely. (SIMD instructions lowering CPU frequency and Dynamically determining where a rogue AVX-512 instruction is executing) Presumably Ice Lake is similar. ( Workaround: not a problem for zmm16..31 , according to @BeeOnRope's comments which I quoted in Is it useful to use VZEROUPPER if your program+libraries contain no SSE instructions? So this strlen could just use vpxord xmm16,xmm16,xmm16

Convert between big-endian and little-endian on RISC-V

随声附和 提交于 2020-05-08 14:37:50
问题 What is the simplest way to work with big-endian values in RISC-V at the assembly language level? That is, how to load a big-endian value from memory into a register, work with the register value in native-endian (little-endian), then store it back into memory in big-endian. 16, 32 and 64 bit values are used in many network protocols and file formats. I couldn't find a byte-swap instruction (equivalent to BSWAP on x86 or REV on ARM) in the manual, nor anything about big-endian loads and