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 6794953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:17:03+00:00 2026-05-26T18:17:03+00:00

I use Intel x86 for assembler programming. I’ve got two variables (int), and I

  • 0

I use Intel x86 for assembler programming. I’ve got two variables (int), and I want the assembler function to return the biggest. I call the assembler function with a C program, and I’ve got this in the main(), function(1,5).

Here is the assembler code:

            .globl function

            .data
var1:       .long 0
var2:       .long 0

            .text
function:
            movl    4(%esp), %eax
            movl    8(%esp), %ebx

            cmp     %eax, %ebx
            jg      cond1          /*greater, if a < b */
            jl      cond2          /*lower, if a > b */

            movl    var2, %eax

            ret

cond1:
            movl    %eax, var1     /*var1 = a */
            movl    %ebx, var2     /*var2 = b */
            ret


cond2:
            movl    %eax, var2     /*var2 = a*/
            movl    %ebx, var1     /*var1 = b */
            ret

The biggest number will be in %eax (movl var2, %eax). The problem is that
the function always returns the initial number in %eax.
For example, function(1,5) returns “1” instead of “5”.

I don’t understand why the result is wrong.

EDIT : Thanks to your replies, I’ve modified the program thanks to your advice :

  function:
            movl    4(%esp), %eax
            movl    8(%esp), %ebx

            cmp     %eax, %ebx
            jg      cond1          /*greater, if a < b */
            jl      cond2          /*lower, if a > b */
            next:
            movl    var2, %eax
            ret

cond1:
            movl    %eax, var1     /*var1 = a */
            movl    %ebx, var2     /*var2 = b */
            jmp     next

cond2:
            movl    %eax, var2     /*var2 = a*/
            movl    %ebx, var1     /*var1 = b */
            jmp     next

To come-back in function(), I use jmp, is it correct?
It works fine.

Also, how can I improve this code? I use variables because the aim will be to have three numbers and find the median one.

  • 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-05-26T18:17:04+00:00Added an answer on May 26, 2026 at 6:17 pm

    I think you’re confused about what the jg and jl instructions are doing.

    From your code, my best guess is that you’re thinking of them as being approximately equivalent to this C code:

    if (condition) cond1();
    

    whereas they actually behave like

    if (condition) goto cond1;
    

    So, there are three possible control flow paths through your function:

    1) If the jg branch is taken:

    ----caller----.
                  |
                  v
    
    function:
                 movl   4(%esp), %eax
                 movl   8(%esp), %ebx
    
                 cmp        %eax, %ebx   
                 jg         cond1            /*greater, if a < b */
    
                  |
               branch 
                  |
                  v
    
    cond1:
                 movl       %eax, var1             /*var1 = a */
                 movl       %ebx, var2             /*var2 = b */
                 ret
    
                  |
       return to  |
    <---caller----'
    

    2) If the jg branch is not taken, but the jl branch is taken:

    ----caller----.
                  |
                  v
    
    function:
                 movl   4(%esp), %eax
                 movl   8(%esp), %ebx
    
                 cmp        %eax, %ebx   
                 jg         cond1            /*greater, if a < b */
                 jl         cond2                   /*lower, if a > b */
    
                  |
               branch 
                  |
                  v
    
    cond2:
                 movl       %eax, var2            /*var2 = a*/
                 movl       %ebx, var1            /*var1 = b */
                 ret
    
                  |
       return to  |
    <---caller----'
    

    3) If neither branch is taken — this is the only path which executes movl var2, %eax:

    ----caller----.
                  |
                  v
    
    function:
                 movl   4(%esp), %eax
                 movl   8(%esp), %ebx
    
                 cmp        %eax, %ebx   
                 jg         cond1            /*greater, if a < b */
                 jl         cond2                   /*lower, if a > b */
    
                 movl       var2, %eax
    
                 ret
    
                  |
       return to  |
    <---caller----'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is the following: I want to use the idb intel debugger with
I want to use GCC inline assembly, Intel syntax. Is there an equivalent for
I want to use the Hardware Performance Counters that come with the Intel and
Use case: I've just entered insert mode, and typed some text. Now I want
I use a population count (hamming weight) function intensively in a windows c application
I need to find the find the current date in binary for Intel x86
I know of otool -tv , but I would much rather use the Intel
Are there any resources on x86 32/64-bit assembly programming without macros? The only one
I have a Intel HEX file and I want to have a binary file.
I'm trying to learn assembly (Intel x86 with NASM on Linux) and unsurprisingly I'm

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.