Implementation of functions of the Assembly Language Learning Series and assembly language functions

Source: Internet
Author: User

Implementation of functions of the Assembly Language Learning Series and assembly language functions

The following code is used to demonstrate functions that exchange integers a and B. The compiling environment is Ubuntu14.04 (32-bit)

If you want to implement the following C code

#include <stdio.h>void swap(int *xp, int *yp){    int x = *xp;    int y = *yp;        *xp = y;    *yp = x;}int main(){    int a = 534, b = 1057;    swap(&a, &b);    printf("%d\n", a);    printf("%d\n", b);    return 0;}
  • The Assembly Code is as follows:
. Section. data :. int 534 B :. int 1057 format :. asciz "% d \ n ". section. text. global _ start_start: pushl % ebp movl % esp, % ebp subl $24, % esp # allocate 24 bytes of memory to the stack to save the local variables movl $ a, % eax movl % eax, 4 (% esp) movl $ B, % eax movl % eax, (% esp) call swap # call the swap function pushl a # print a pushl $ format call printf pushl B # print B pushl $ format call printf movl $0, (% esp) call exit swap: pushl % ebp # Save the pre-call stack frame address movl % esp, % ebp pushl % ebx # Save the register ebx movl 8 (% ebp) according to your habits ), % edx # get xp movl 12 (% ebp), % ecx # get yp movl (% edx), % ebx # get x movl (% ecx ), % eax # get y movl % eax, (% edx) movl % ebx, (% ecx) popl % ebx popl % ebp ret
  • Compile

As swap. s-o swap. o

  • Link

Ld-lc-I/lib/ld-linux.so.2 swap. o-o swap

  • Run

./Swap

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.