Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7786717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:27:47+00:00 2026-06-01T20:27:47+00:00

edit: this is using AT&T syntax I am learning assembly using the GAS assembler

  • 0

edit: this is using AT&T syntax

I am learning assembly using the GAS assembler and have to write a program where i compare the int value in an array and change the value based on the comparison. I am writing it using inline assembly in C. I know that for a basic variable, say int i, i store the value in register eax with the following line:

movl i, %eax

but now say i have a variable a[2][2] and i want to move a[1][1] into %eax. the obvious wrong answer is:

movl a[1][1], %eax

and i get the error junk ‘[1][1]’ after the expression. How do i go about moving the value of a 2d array into a register? thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:27:47+00:00Added an answer on June 1, 2026 at 8:27 pm

    It’s up to you to turn the a[1][1] into a linear address. In other words, we have to take a[1][1] and figure out how far that is from the beginning of a. We start by figuring the size of a row — in this case, 2 ints. So, (working in ints, for the moment) the beginning of row 0 is at offset 0 from a, row 1 at offset 2, and (if there were more rows) so on. We then add on the offset within that row. Finally, we scale that by the size of a single item.

    From there, we have a couple of possibilities. One is that we just need a fixed position — we’re going to get a[1][1], no matter what. The other is that we’re really looking at a[i][j], where i and j happen to be 1 at the moment, but could be other sizes.

    For the first, we can use the fact that the assembler can do some math to compute the address for us.

    // one row down times 2 ints per row + offset of 1 into last row, all times size of int
    movl a + (1 * 2 + 1)*4, eax
    

    In the second case, let’s assume we have i in esi and j in ebx. In this case, we have to do the math ourselves (sorry, for the moment I’m going to use Intel syntax — I’m just a lot more accustomed to it):

    shl esi, 1    ; i * 2
    add esi, ebx  ; i * 2 + j
    shl esi, 2    ;(i * 2 + j) * 4
    
    mov eax, a[esi]
    

    The x86 can actually combine some operations like this that are common for addressing, so you don’t have to execute them as separate instructions as I have above, so we could pretty easily reduce that to:

    shl esi, 1
    mov eax, a[esi][ebx]
    

    That last may require a bit of explanation — at least with MASM (and probably with gas, I’d guess) the assembler knows that since you’re loading a value into eax that you’re working in 4-byte quantities, so it automatically scales the offset by 4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: See this in action here: http://jsbin.com/emobi/5 -- and that's using mouseenter/mouseleave. I have
Sorry, this is my first time using this forum. Apparently people can edit my
[edit] I am NOT using jquery in this app. Looking for a way to
[NOTE: I'm using ASP.NET MVC2 RC2.] I have URLs like this: /customers/123/orders/456/items/index /customers/123/orders/456/items/789/edit My
This is a weird problem. I am using a dataform, and when I edit
EDIT : This has begun working and I have not changed anything, this is
Edit: This HTML is output from a Flash application & its badly formed because
EDIT: This post was originally specific to ASP.NET, but after thinking about it I'm
Edit This question has gone through a few iterations by now, so feel free
EDIT: This question is a duplicate of What is the difference between managed and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.