English 中文(简体)
为什么从字节数组装入 EAX 不和我期望的字面数组相比?
原标题:Why does loading EAX from a byte array not compare equal to the literal I expected?
I am new to assembly and was trying to work with an array. However, I was geting caught when try to compare the array value to a constant stored in a register. From the following code I was expecting the program to jump after comparison because the value in the array seems to match the literal in esi. My code is as follows. global _start section .data yay: db "It Worked!",0xA array1: db 1, 2, 3, 4 section .bss section .text _start: mov eax, [array1 + 1] mov esi, 2 cmp eax, esi je .equal jmp _done .equal: mov eax, 4 mov ebx, 0 mov ecx, yay mov edx, 11 int 0x80 jmp _done _done: mov eax, 1 mov ebx, 0 int 0x80 Any advice as to mistakes in my code are greatly appreciated too!
问题回答
The reason your code is not properly loading the value located at the first index of your array into EAX is because of what an array is in assembly. In assembly, when you define an "array" you are simply placing data into memory. While this might seem obvious, it also means that there is NOT a separator between elements (A "string" is also an array). This means that when you do MOV EAX, [ARRAY1 + 1] without specifying the size of the operation, nasm looks at the context of the instruction to deffer the size. Because you are using a 32-bit register, the size is defaulted to a DWORD. This means that the instruction will load 32 bits (4 bytes) from your array starting at [ARRAY1 + 1]. To prevent this, you need to specify the size of your operation as a byte by doing MOVZX EAX, BYTE [ARRAY1 + 1]. You need to use MOVZX to fill the rest of the register with zeros. General Tips The first thing that sticks out to me about your code is the usage of JMP right before the label you are jumping too. This is unnecessary because execution will just continue into a label. This is because labels are not "functions," rather just markers that represent memory addresses. They do not exist in the assembled program. Secondly, while not major, instead of doing MOV EAX, 0, it is slightly faster to execute XOR EAX, EAX. Finaly, you have an empty BSS section, this is not needed for the linker and can just be left out (at least for ld).




相关问题
WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Convert a 2D array index into a 1D index

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it ...

Convert an array of integers for use in a SQL "IN" clause

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause? e.g. int[] values = {1,2,3}; would go to "(1,2,3)"

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

Best practice of big javascript objects

sry for this imprecise topic name. I am querying a dataset a lot of times so using ajax request would end up in tons of http requests. For this reason I decided to use the json encode method to ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签