Assigning value to array element in assembly (GAS syntax)












0















I have the following C code that returns 9:



#include <stdio.h>
int main()
{
int array[4]={0};
array[2]=9;
return array[2];
}


To accomplish the same in assembly language I have tried:



.section .data
array: .zero 4
.section .text
.globl _start
_start:
mov $2,%esi
mov array(,%esi,4),%ecx # copy address of array[2] to %ecx
mov $9,%ecx
mov $1,%eax
mov array(,%esi,4),%ebx # return the value of array[2]
int $0x80


assembled and linked with:



gcc -g -c array.s && ld array.o && ./a.out


However this program returns 0 instead of 9:



>./a.out
>echo $?
0


I suspect the problem is in the lines with comments. After various unsuccessful attempts to find the problem I decided to ask the question: How to change of the value an array element (in this case array[2]) in assembly?










share|improve this question




















  • 2





    mov array(,%esi,4),%ecx # copy address of array[2] to %ecx - this loads value from memory. To get address in similar way one can use lea array(,%esi,4),%ecx. And mov $9,%ecx loads value 9 into ecx, it does not involve any extra memory access except the instruction opcode itself. And your final mov array(,%esi,4),%ebx is completely correct and doing what it should, but if you will check how it differs from that "move address to ecx", there's no difference, so I'm not sure why you expect different result. (also using debugger to watch CPU state per instruction is of tremendous help)

    – Ped7g
    Nov 23 '18 at 15:50








  • 1





    And to modify array in fixed way, you can skip all those extra preparations, and just do immediately movl $9, array+2*4 to use the MOV r/m32, imm32 instruction to write immediate directly into memory. If you insist on indexing (to learn it for later), then movl $9, array(,%esi,4) writes value 9 into memory at "array + esi*4". To use "address" in ecx (if it would hold one), you can do movl $9,(%ecx), to write into memory, the parentheses make it memory operand, not register, as target.

    – Ped7g
    Nov 23 '18 at 15:54













  • Thanks, now I see the flaws with the code I posted. Both movl $9, array+2*4 and movl $9,array(,%esi,4) give the desired result.

    – Sergio
    Nov 23 '18 at 16:46


















0















I have the following C code that returns 9:



#include <stdio.h>
int main()
{
int array[4]={0};
array[2]=9;
return array[2];
}


To accomplish the same in assembly language I have tried:



.section .data
array: .zero 4
.section .text
.globl _start
_start:
mov $2,%esi
mov array(,%esi,4),%ecx # copy address of array[2] to %ecx
mov $9,%ecx
mov $1,%eax
mov array(,%esi,4),%ebx # return the value of array[2]
int $0x80


assembled and linked with:



gcc -g -c array.s && ld array.o && ./a.out


However this program returns 0 instead of 9:



>./a.out
>echo $?
0


I suspect the problem is in the lines with comments. After various unsuccessful attempts to find the problem I decided to ask the question: How to change of the value an array element (in this case array[2]) in assembly?










share|improve this question




















  • 2





    mov array(,%esi,4),%ecx # copy address of array[2] to %ecx - this loads value from memory. To get address in similar way one can use lea array(,%esi,4),%ecx. And mov $9,%ecx loads value 9 into ecx, it does not involve any extra memory access except the instruction opcode itself. And your final mov array(,%esi,4),%ebx is completely correct and doing what it should, but if you will check how it differs from that "move address to ecx", there's no difference, so I'm not sure why you expect different result. (also using debugger to watch CPU state per instruction is of tremendous help)

    – Ped7g
    Nov 23 '18 at 15:50








  • 1





    And to modify array in fixed way, you can skip all those extra preparations, and just do immediately movl $9, array+2*4 to use the MOV r/m32, imm32 instruction to write immediate directly into memory. If you insist on indexing (to learn it for later), then movl $9, array(,%esi,4) writes value 9 into memory at "array + esi*4". To use "address" in ecx (if it would hold one), you can do movl $9,(%ecx), to write into memory, the parentheses make it memory operand, not register, as target.

    – Ped7g
    Nov 23 '18 at 15:54













  • Thanks, now I see the flaws with the code I posted. Both movl $9, array+2*4 and movl $9,array(,%esi,4) give the desired result.

    – Sergio
    Nov 23 '18 at 16:46
















0












0








0








I have the following C code that returns 9:



#include <stdio.h>
int main()
{
int array[4]={0};
array[2]=9;
return array[2];
}


To accomplish the same in assembly language I have tried:



.section .data
array: .zero 4
.section .text
.globl _start
_start:
mov $2,%esi
mov array(,%esi,4),%ecx # copy address of array[2] to %ecx
mov $9,%ecx
mov $1,%eax
mov array(,%esi,4),%ebx # return the value of array[2]
int $0x80


assembled and linked with:



gcc -g -c array.s && ld array.o && ./a.out


However this program returns 0 instead of 9:



>./a.out
>echo $?
0


I suspect the problem is in the lines with comments. After various unsuccessful attempts to find the problem I decided to ask the question: How to change of the value an array element (in this case array[2]) in assembly?










share|improve this question
















I have the following C code that returns 9:



#include <stdio.h>
int main()
{
int array[4]={0};
array[2]=9;
return array[2];
}


To accomplish the same in assembly language I have tried:



.section .data
array: .zero 4
.section .text
.globl _start
_start:
mov $2,%esi
mov array(,%esi,4),%ecx # copy address of array[2] to %ecx
mov $9,%ecx
mov $1,%eax
mov array(,%esi,4),%ebx # return the value of array[2]
int $0x80


assembled and linked with:



gcc -g -c array.s && ld array.o && ./a.out


However this program returns 0 instead of 9:



>./a.out
>echo $?
0


I suspect the problem is in the lines with comments. After various unsuccessful attempts to find the problem I decided to ask the question: How to change of the value an array element (in this case array[2]) in assembly?







assembly gas att






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 15:49







Sergio

















asked Nov 23 '18 at 15:43









SergioSergio

3,3563927




3,3563927








  • 2





    mov array(,%esi,4),%ecx # copy address of array[2] to %ecx - this loads value from memory. To get address in similar way one can use lea array(,%esi,4),%ecx. And mov $9,%ecx loads value 9 into ecx, it does not involve any extra memory access except the instruction opcode itself. And your final mov array(,%esi,4),%ebx is completely correct and doing what it should, but if you will check how it differs from that "move address to ecx", there's no difference, so I'm not sure why you expect different result. (also using debugger to watch CPU state per instruction is of tremendous help)

    – Ped7g
    Nov 23 '18 at 15:50








  • 1





    And to modify array in fixed way, you can skip all those extra preparations, and just do immediately movl $9, array+2*4 to use the MOV r/m32, imm32 instruction to write immediate directly into memory. If you insist on indexing (to learn it for later), then movl $9, array(,%esi,4) writes value 9 into memory at "array + esi*4". To use "address" in ecx (if it would hold one), you can do movl $9,(%ecx), to write into memory, the parentheses make it memory operand, not register, as target.

    – Ped7g
    Nov 23 '18 at 15:54













  • Thanks, now I see the flaws with the code I posted. Both movl $9, array+2*4 and movl $9,array(,%esi,4) give the desired result.

    – Sergio
    Nov 23 '18 at 16:46
















  • 2





    mov array(,%esi,4),%ecx # copy address of array[2] to %ecx - this loads value from memory. To get address in similar way one can use lea array(,%esi,4),%ecx. And mov $9,%ecx loads value 9 into ecx, it does not involve any extra memory access except the instruction opcode itself. And your final mov array(,%esi,4),%ebx is completely correct and doing what it should, but if you will check how it differs from that "move address to ecx", there's no difference, so I'm not sure why you expect different result. (also using debugger to watch CPU state per instruction is of tremendous help)

    – Ped7g
    Nov 23 '18 at 15:50








  • 1





    And to modify array in fixed way, you can skip all those extra preparations, and just do immediately movl $9, array+2*4 to use the MOV r/m32, imm32 instruction to write immediate directly into memory. If you insist on indexing (to learn it for later), then movl $9, array(,%esi,4) writes value 9 into memory at "array + esi*4". To use "address" in ecx (if it would hold one), you can do movl $9,(%ecx), to write into memory, the parentheses make it memory operand, not register, as target.

    – Ped7g
    Nov 23 '18 at 15:54













  • Thanks, now I see the flaws with the code I posted. Both movl $9, array+2*4 and movl $9,array(,%esi,4) give the desired result.

    – Sergio
    Nov 23 '18 at 16:46










2




2





mov array(,%esi,4),%ecx # copy address of array[2] to %ecx - this loads value from memory. To get address in similar way one can use lea array(,%esi,4),%ecx. And mov $9,%ecx loads value 9 into ecx, it does not involve any extra memory access except the instruction opcode itself. And your final mov array(,%esi,4),%ebx is completely correct and doing what it should, but if you will check how it differs from that "move address to ecx", there's no difference, so I'm not sure why you expect different result. (also using debugger to watch CPU state per instruction is of tremendous help)

– Ped7g
Nov 23 '18 at 15:50







mov array(,%esi,4),%ecx # copy address of array[2] to %ecx - this loads value from memory. To get address in similar way one can use lea array(,%esi,4),%ecx. And mov $9,%ecx loads value 9 into ecx, it does not involve any extra memory access except the instruction opcode itself. And your final mov array(,%esi,4),%ebx is completely correct and doing what it should, but if you will check how it differs from that "move address to ecx", there's no difference, so I'm not sure why you expect different result. (also using debugger to watch CPU state per instruction is of tremendous help)

– Ped7g
Nov 23 '18 at 15:50






1




1





And to modify array in fixed way, you can skip all those extra preparations, and just do immediately movl $9, array+2*4 to use the MOV r/m32, imm32 instruction to write immediate directly into memory. If you insist on indexing (to learn it for later), then movl $9, array(,%esi,4) writes value 9 into memory at "array + esi*4". To use "address" in ecx (if it would hold one), you can do movl $9,(%ecx), to write into memory, the parentheses make it memory operand, not register, as target.

– Ped7g
Nov 23 '18 at 15:54







And to modify array in fixed way, you can skip all those extra preparations, and just do immediately movl $9, array+2*4 to use the MOV r/m32, imm32 instruction to write immediate directly into memory. If you insist on indexing (to learn it for later), then movl $9, array(,%esi,4) writes value 9 into memory at "array + esi*4". To use "address" in ecx (if it would hold one), you can do movl $9,(%ecx), to write into memory, the parentheses make it memory operand, not register, as target.

– Ped7g
Nov 23 '18 at 15:54















Thanks, now I see the flaws with the code I posted. Both movl $9, array+2*4 and movl $9,array(,%esi,4) give the desired result.

– Sergio
Nov 23 '18 at 16:46







Thanks, now I see the flaws with the code I posted. Both movl $9, array+2*4 and movl $9,array(,%esi,4) give the desired result.

– Sergio
Nov 23 '18 at 16:46














1 Answer
1






active

oldest

votes


















0














.section .data
array: .zero 16 # allocate 16 bytes instead of 4
.section .text
.globl _start
_start:
mov $2,%esi
lea array(,%esi,4),%ecx # copy address of array[2] to %ecx, not the value
mov $9, %ebx
mov %ebx,(%ecx) # assign value into the memory pointed by %ecx
mov $1,%eax
mov array(,%esi,4),%ebx
int $0x80





share|improve this answer


























  • The line mov $9,(%ecx) gives the error: no instruction mnemonic suffix given and no register operands;

    – Sergio
    Nov 23 '18 at 16:44











  • Put the value in a register (%ebx) before moving it in memory, or just add l suffix to the mnemonic: movl $9, (%ecx)

    – Nathan Xabedi
    Nov 23 '18 at 16:57








  • 1





    mov $9,(%ecx) is ambiguous, the assembler doesn't know if byte, word or dword should be written in memory (all three options are possible in x86 32 bit mode). The AT&T syntax is mostly used with suffix form of instructions like movl / movw / movb /..., but as long as there's no ambiguity, the gas will accept also missing suffix, like OP original code has everywhere. Writing suffix form everywhere is a bit more writing (and I personally have harder time to read it), but it may help to catch some bugs, like you want to store word into memory, and you write movw %eax,(%ebp) = eax error.

    – Ped7g
    Nov 23 '18 at 22:12













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449516%2fassigning-value-to-array-element-in-assembly-gas-syntax%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














.section .data
array: .zero 16 # allocate 16 bytes instead of 4
.section .text
.globl _start
_start:
mov $2,%esi
lea array(,%esi,4),%ecx # copy address of array[2] to %ecx, not the value
mov $9, %ebx
mov %ebx,(%ecx) # assign value into the memory pointed by %ecx
mov $1,%eax
mov array(,%esi,4),%ebx
int $0x80





share|improve this answer


























  • The line mov $9,(%ecx) gives the error: no instruction mnemonic suffix given and no register operands;

    – Sergio
    Nov 23 '18 at 16:44











  • Put the value in a register (%ebx) before moving it in memory, or just add l suffix to the mnemonic: movl $9, (%ecx)

    – Nathan Xabedi
    Nov 23 '18 at 16:57








  • 1





    mov $9,(%ecx) is ambiguous, the assembler doesn't know if byte, word or dword should be written in memory (all three options are possible in x86 32 bit mode). The AT&T syntax is mostly used with suffix form of instructions like movl / movw / movb /..., but as long as there's no ambiguity, the gas will accept also missing suffix, like OP original code has everywhere. Writing suffix form everywhere is a bit more writing (and I personally have harder time to read it), but it may help to catch some bugs, like you want to store word into memory, and you write movw %eax,(%ebp) = eax error.

    – Ped7g
    Nov 23 '18 at 22:12


















0














.section .data
array: .zero 16 # allocate 16 bytes instead of 4
.section .text
.globl _start
_start:
mov $2,%esi
lea array(,%esi,4),%ecx # copy address of array[2] to %ecx, not the value
mov $9, %ebx
mov %ebx,(%ecx) # assign value into the memory pointed by %ecx
mov $1,%eax
mov array(,%esi,4),%ebx
int $0x80





share|improve this answer


























  • The line mov $9,(%ecx) gives the error: no instruction mnemonic suffix given and no register operands;

    – Sergio
    Nov 23 '18 at 16:44











  • Put the value in a register (%ebx) before moving it in memory, or just add l suffix to the mnemonic: movl $9, (%ecx)

    – Nathan Xabedi
    Nov 23 '18 at 16:57








  • 1





    mov $9,(%ecx) is ambiguous, the assembler doesn't know if byte, word or dword should be written in memory (all three options are possible in x86 32 bit mode). The AT&T syntax is mostly used with suffix form of instructions like movl / movw / movb /..., but as long as there's no ambiguity, the gas will accept also missing suffix, like OP original code has everywhere. Writing suffix form everywhere is a bit more writing (and I personally have harder time to read it), but it may help to catch some bugs, like you want to store word into memory, and you write movw %eax,(%ebp) = eax error.

    – Ped7g
    Nov 23 '18 at 22:12
















0












0








0







.section .data
array: .zero 16 # allocate 16 bytes instead of 4
.section .text
.globl _start
_start:
mov $2,%esi
lea array(,%esi,4),%ecx # copy address of array[2] to %ecx, not the value
mov $9, %ebx
mov %ebx,(%ecx) # assign value into the memory pointed by %ecx
mov $1,%eax
mov array(,%esi,4),%ebx
int $0x80





share|improve this answer















.section .data
array: .zero 16 # allocate 16 bytes instead of 4
.section .text
.globl _start
_start:
mov $2,%esi
lea array(,%esi,4),%ecx # copy address of array[2] to %ecx, not the value
mov $9, %ebx
mov %ebx,(%ecx) # assign value into the memory pointed by %ecx
mov $1,%eax
mov array(,%esi,4),%ebx
int $0x80






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 16:55

























answered Nov 23 '18 at 16:26









Nathan XabediNathan Xabedi

1036




1036













  • The line mov $9,(%ecx) gives the error: no instruction mnemonic suffix given and no register operands;

    – Sergio
    Nov 23 '18 at 16:44











  • Put the value in a register (%ebx) before moving it in memory, or just add l suffix to the mnemonic: movl $9, (%ecx)

    – Nathan Xabedi
    Nov 23 '18 at 16:57








  • 1





    mov $9,(%ecx) is ambiguous, the assembler doesn't know if byte, word or dword should be written in memory (all three options are possible in x86 32 bit mode). The AT&T syntax is mostly used with suffix form of instructions like movl / movw / movb /..., but as long as there's no ambiguity, the gas will accept also missing suffix, like OP original code has everywhere. Writing suffix form everywhere is a bit more writing (and I personally have harder time to read it), but it may help to catch some bugs, like you want to store word into memory, and you write movw %eax,(%ebp) = eax error.

    – Ped7g
    Nov 23 '18 at 22:12





















  • The line mov $9,(%ecx) gives the error: no instruction mnemonic suffix given and no register operands;

    – Sergio
    Nov 23 '18 at 16:44











  • Put the value in a register (%ebx) before moving it in memory, or just add l suffix to the mnemonic: movl $9, (%ecx)

    – Nathan Xabedi
    Nov 23 '18 at 16:57








  • 1





    mov $9,(%ecx) is ambiguous, the assembler doesn't know if byte, word or dword should be written in memory (all three options are possible in x86 32 bit mode). The AT&T syntax is mostly used with suffix form of instructions like movl / movw / movb /..., but as long as there's no ambiguity, the gas will accept also missing suffix, like OP original code has everywhere. Writing suffix form everywhere is a bit more writing (and I personally have harder time to read it), but it may help to catch some bugs, like you want to store word into memory, and you write movw %eax,(%ebp) = eax error.

    – Ped7g
    Nov 23 '18 at 22:12



















The line mov $9,(%ecx) gives the error: no instruction mnemonic suffix given and no register operands;

– Sergio
Nov 23 '18 at 16:44





The line mov $9,(%ecx) gives the error: no instruction mnemonic suffix given and no register operands;

– Sergio
Nov 23 '18 at 16:44













Put the value in a register (%ebx) before moving it in memory, or just add l suffix to the mnemonic: movl $9, (%ecx)

– Nathan Xabedi
Nov 23 '18 at 16:57







Put the value in a register (%ebx) before moving it in memory, or just add l suffix to the mnemonic: movl $9, (%ecx)

– Nathan Xabedi
Nov 23 '18 at 16:57






1




1





mov $9,(%ecx) is ambiguous, the assembler doesn't know if byte, word or dword should be written in memory (all three options are possible in x86 32 bit mode). The AT&T syntax is mostly used with suffix form of instructions like movl / movw / movb /..., but as long as there's no ambiguity, the gas will accept also missing suffix, like OP original code has everywhere. Writing suffix form everywhere is a bit more writing (and I personally have harder time to read it), but it may help to catch some bugs, like you want to store word into memory, and you write movw %eax,(%ebp) = eax error.

– Ped7g
Nov 23 '18 at 22:12







mov $9,(%ecx) is ambiguous, the assembler doesn't know if byte, word or dword should be written in memory (all three options are possible in x86 32 bit mode). The AT&T syntax is mostly used with suffix form of instructions like movl / movw / movb /..., but as long as there's no ambiguity, the gas will accept also missing suffix, like OP original code has everywhere. Writing suffix form everywhere is a bit more writing (and I personally have harder time to read it), but it may help to catch some bugs, like you want to store word into memory, and you write movw %eax,(%ebp) = eax error.

– Ped7g
Nov 23 '18 at 22:12




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449516%2fassigning-value-to-array-element-in-assembly-gas-syntax%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'