assembly

x86 Assembly: Before Making a System Call on Linux Should You Save All Registers?

久未见 提交于 2020-02-10 08:58:41
问题 I have the below code that opens up a file, reads it into a buffer and then closes the file. The close file system call requires that the file descriptor number be in the ebx register. The ebx register gets the file descriptor number before the read system call is made. My question is should I save the ebx register on the stack or somewhere before I make the read system call, (could int 80h trash the ebx register?). And then restore the ebx register for the close system call? Or is the code I

Reproduce these C types in assembly?

依然范特西╮ 提交于 2020-02-08 09:58:33
问题 I am trying to reproduce two opaque data types from the pthreads library in NASM. These data types are pthread_attr_t and cpu_set_t from pthread_attr_setaffinity_np (see http://man7.org/linux/man-pages/man3/pthread_attr_setaffinity_np.3.html). I created a simple C program to call pthread_attr_setaffinity_np and stepped through it with gdb to examine the format of those two bitmasks (pthread_attr_t is an affinity mask). When I debug the C version with gdb, I print the values of attr and cpus:

Assembly x86: splitting string based on delimiter

那年仲夏 提交于 2020-02-08 07:01:44
问题 Goal: Take string 'One Two Three' which is stored in EDI and call strtok. strtok should split the string so that ESI points to the rest of the string after some delimiter and the first part of the string before the delimiter is stored in a register. After Call strtok with the delimiter of ' ' the result should be ESI = 'Two Three' and some other register = 'One' Using the code below i can find the index of the first instance of a space ' ' in string stored at EDI. index at the end is stored

Assembly x86: splitting string based on delimiter

倖福魔咒の 提交于 2020-02-08 07:01:33
问题 Goal: Take string 'One Two Three' which is stored in EDI and call strtok. strtok should split the string so that ESI points to the rest of the string after some delimiter and the first part of the string before the delimiter is stored in a register. After Call strtok with the delimiter of ' ' the result should be ESI = 'Two Three' and some other register = 'One' Using the code below i can find the index of the first instance of a space ' ' in string stored at EDI. index at the end is stored

Is this piece of assembly code invalid?

泪湿孤枕 提交于 2020-02-08 06:40:11
问题 I'm trying to figure out whether the following piece of assembly code is invalid. movb $0xF, (%bl) Is it invalid? If so, why? Thanks. 回答1: You don't say what processor. bl is a 8-bit register at least in x86 processors, but it cannot be used for addressing. Why is it invalid instruction? Well, the reason an assembly instruction is invalid is that there's no such instruction for the given processor. There is no possible way to encode this instruction. In this case (assuming x86), using bl or

Is this piece of assembly code invalid?

梦想的初衷 提交于 2020-02-08 06:40:05
问题 I'm trying to figure out whether the following piece of assembly code is invalid. movb $0xF, (%bl) Is it invalid? If so, why? Thanks. 回答1: You don't say what processor. bl is a 8-bit register at least in x86 processors, but it cannot be used for addressing. Why is it invalid instruction? Well, the reason an assembly instruction is invalid is that there's no such instruction for the given processor. There is no possible way to encode this instruction. In this case (assuming x86), using bl or

Generating and catching invalid instruction

亡梦爱人 提交于 2020-02-07 06:57:12
问题 I want to isolate a custom opcode in my program in order to catch invalid opcode exception if there is any. If I write an empty main, I see an assembly sequence as below: (gdb) list 1 void main() 2 { 3 } (gdb) disass /r main Dump of assembler code for function main: 0x00000000004004ed <+0>: 55 push %rbp 0x00000000004004ee <+1>: 48 89 e5 mov %rsp,%rbp 0x00000000004004f1 <+4>: 5d pop %rbp 0x00000000004004f2 <+5>: c3 retq End of assembler dump. If I write a single random byte such as __asm__("

Convert a decimal string to a signed 2's complement binary integer

二次信任 提交于 2020-02-07 03:41:37
问题 I was just wondering what the code would be to convert a decimal integer (entered in by the user) to a 2's compliment representation in memory. Example: the user enters 1234 as the decimal. I want to store 04D2 (the binary 2's complement representation) in AX (since I'm only allowed to use word length strings) I'm using 80x86 architecture. 回答1: Converting a string of digits to an integer is mostly fairly simple: you read one digit at a time, convert that to a decimal number (normally by

Convert a decimal string to a signed 2's complement binary integer

时间秒杀一切 提交于 2020-02-07 03:40:11
问题 I was just wondering what the code would be to convert a decimal integer (entered in by the user) to a 2's compliment representation in memory. Example: the user enters 1234 as the decimal. I want to store 04D2 (the binary 2's complement representation) in AX (since I'm only allowed to use word length strings) I'm using 80x86 architecture. 回答1: Converting a string of digits to an integer is mostly fairly simple: you read one digit at a time, convert that to a decimal number (normally by

Why is default operand size 32 bits in 64 mode?

烂漫一生 提交于 2020-02-06 07:55:36
问题 I am reading Intel doc, vol. 1 and There is a chapter of 3.6.1 Operand Size and Address Size in 64-Bit Mode . There are three prefixes REX.W , operand-size 66 and address-size 67 prefix. And there is mentioned that operand are defaulted to be 32 bit in size. And is only possible to change it with REX.W instruction prefix (after other prefixes) to make it 64 bits long. I do not know why so, why cannot I used the full 64 bit space for example for int operand? Does it have something to do with