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

  • SEARCH
  • Home
  • 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 7946163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:04:17+00:00 2026-06-04T01:04:17+00:00

I am currently writing a simple C compiler, that takes a .c file as

  • 0

I am currently writing a simple C compiler, that takes a .c file as input and generates assembly code (X86, AT&T syntax). I am having a hard time passing array-type parameters and generating the correct assembly code for it. Here’s my input:

int getIndexOne(int tab[]){
  return tab[1];
}

int main_test(void){
  int t[3];
  t[0] = 0;
  t[1] = 1;
  t[2] = 2;
  return getIndexOne(t);
}

A fairly simple test. Here is my output:

getIndexOne:
.LFB0:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 16
    movl    %esp, %ebp
    .cfi_offset 6, -16
    .cfi_def_cfa_register 6
    movl    %edi, -24(%ebp)
    movl    $1, %eax
    movl    -24(%ebp, %eax, 8), %eax    #trouble over here
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size getIndexOne, .-getIndexOne


falsemain:
.LFB1:
    .cfi_startproc
    pushl   %ebp
    .cfi_def_cfa_offset 16
    movl    %esp, %ebp
    .cfi_offset 6, -16
    .cfi_def_cfa_register 6
    pushl   %ebx
    subl    $120, %esp
    movl    $2, -32(%ebp)
    movl    $0, %eax
    movl    $0, -24(%ebp, %eax, 8)
    movl    $1, %eax
    movl    $1, -24(%ebp, %eax, 8)
    movl    $2, %eax
    movl    $2, -24(%ebp, %eax, 8)
    leal    -24(%ebp, %eax, 8), %eax
    movl    %eax, %edi
    call    getIndexOne
    addl    $120, %esp
    popl    %ebx
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE1:
    .size main_test, .-main_test

I’m unable to access the content of the passed address (leal instruction). Any help would be much appreciated.
PS: Don’t worry about the size of my ints, they are set to 8 instead of 4 bytes for other reasons.

  • 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-04T01:04:19+00:00Added an answer on June 4, 2026 at 1:04 am

    There are two problems, the first being when you setup the stack for the call to getIndexOne:

    movl    $2, %eax
    movl    $2, -24(%ebp, %eax, 8)
    leal    -24(%ebp, %eax, 8), %eax   ##<== EAX still holds value of 2!
    movl    %eax, %edi                 ##<== wrong address for start of array
    

    You’re not clearing the contents of the EAX register after the MOV command, therefore the address that you’re putting into the EDI register for the function call is not pointing to the start of the array, but last element of the array (i.e., the second index which has an element value of 2).

    The second problem comes here in your getIndexOne function:

    movl    %edi, -24(%ebp)
    movl    $1, %eax
    movl    -24(%ebp, %eax, 8), %eax
    

    You’ve stored the address on the stack. That’s fine, but it also means when you retreive the value from the stack, that you’re getting a pointer back which must then be dereferenced a second time. What you’re doing right now is you’re just reading back a value offset from the frame-pointer on the stack … that’s not the value in the array since you’re not dereferencing the pointer that you stored on the stack. In other words you should change it to the following if you must store the pointer on the stack (I think this isn’t the most efficient way since the value is already in EDI, but whatever):

    movl    %edi, -24(%ebp)           ##<== store pointer to the array on the stack
    movl    $1, %eax
    movl    -24(%ebp), %ecx           ##<== get the pointer back from the stack
    movl    (%ecx, %eax, 8), %eax     ##<== dereference the pointer
    

    As a side note, while I’m not sure how your compiler works, I do think it’s a little scary that you are using the value that you are loading into the array elements to also index into the array … if the values being loaded don’t match the array index, that’s going to create quite a bit of havok … I’m guessing this is some type of optimization you’re attempting to-do when the two values match?

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

Sidebar

Related Questions

I'm currently writing a simple FTP client (using C# and .NET). It's important that
I'm currently writing a simple game. My graphics code runs once per frame (approximately
I am currently writing a simple, timer-based mini app in C# that performs an
I'm currently writing a simple .sh script to parse an Exim log file for
I'm currently writing a simple batch script to set the DNS of a LAN
I'm currently writing a very simple game engine for an assignment and to make
I'm writing a simple shopping cart application and have hit a road block. Currently
I'm currently writing a Java app that interfaces with a MySQL database. My problem
I am currently writing code in C++ to find all possible permutations of 6
I'm currently writing a simple multicaster module. Only one process can open a proc

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.