Multiplication Table Example

Source: Internet
Author: User

Let's write a simple application This prompts the user for an integer, multiplies it by ascending
Powers of 2 (from + to) using bit shifting, and redisplays each product with leading padded
Spaces. We'll use C + + for the input-output. The assembly language module would contain calls
To three functions written in C + +. The program is launched from C + +.
Assembly Language Module
The assembly language module contains one function, named displaytable. It calls a C + +
function named Askforinteger that inputs a integer from the user. It uses a loop to repeatedly
Shift an integer named Intval to the left and display it by calling Showint.

;ASM function called from C + +INCLUDE Irvine32.Inc;External C + + functions:askforinteger PROTO cshowint PROTO C,Value:Sdword,Outwidth:DWORD;newLine PROTO COut_width =8Ending_power =Ten. Dataintval DWORD?. Code;---------------------------------------------settextoutcolor PROC C,Color:DWORD;;sets the text colors and clears the console;window. Calls Irvine32 library functions.;---------------------------------------------movEax,colorPagerSetTextColorPagerCLRSCRretSettextoutcolor ENDP;---------------------------------------------displaytable PROC C;;Inputs an integer n and displays a;multiplication table ranging from n * 2^1;To N * 2^10.;----------------------------------------------INVOKE Askforinteger;Call C + + functionmovIntval,eax;Save the integermovEcx,ending_power;Loop CounterL1: PushEcx;Save Loop CounterSHLIntval,1 ;Multiply by 2INVOKE showint,intval,out_width;INVOKE newLine; output cr/lfPopEcx;Restore Loop counterLoop L1retdisplaytable endpend

In displaytable, ECX must is pushed and popped before calling Showint and NewLine because
Visual C + + functions do no T save and restore general-purpose registers. The Askforinteger
function returns its result in the EAX register.
Displaytable is not required to use INVOKE when calling the C + + functions. The same
result could is achieved using PUSH and call instructions. This is the Showint
would look:

Push ; push last argument first Push intval Pager ; Call the function add ESP,8; Clean up stack

You must follow the C language calling convention, which arguments is pushed on the
Stack in reverse order and the caller are responsible for removing arguments from the stack after
The call.

C + + Startup program
Let's look at the C + + module, starts the program. Its entry point is main (), ensuring the execution
of required C + + language initialization code. It contains function prototypes for the external
assembly language procedure and the three exported functions:

//main.cpp//demonstrates function calls between a C + + program//and an external assembly language module.#include <iostream>#include<iomanip>using namespacestd;extern "C" {//external ASM procedures:voiddisplaytable ();voidsettextoutcolor (unsigned color);//local C + + functions:intAskforinteger ();voidShowint (intValueintwidth);}//Program Entry PointintMain () {Settextoutcolor (0x1E);//Yellow on BlueDisplaytable ();//Call ASM procedurereturn 0;}//Prompt the user for an integer.intAskforinteger () {intN;cout<<"Enter An integer between 1 and 90,000:"; Cin>>N;returnN;}//Display A signed integer with a specified width.voidShowint (intValueintwidth) {cout<< setw (width) <<value;}

Building the Project our Web site (www.asmirvine.com) have a tutorial for Building combined
c++/assembly Language projects in Visual Studio.

Program output This is sample output generated by the multiplication Table
The user enters 90,000:

Multiplication Table Example

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.