PSC,Heres,tutorial,which,will,teach,basics,so
Quick Search for:  in language:    
PSC,Heres,tutorial,which,will,teach,basics,so
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 451,578 lines
 Jobs: 605 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for C/ C++.
Visual Pi Hex
By jo122321323 on 8/25


Click here to see a screenshot of this code!ShortCutSample
By Massimiliano Tomassetti on 8/25

(Screen Shot)

AnPMoneyManager beta
By Anthony Tristan on 8/24


A calculator (english/polish )
By Tom Dziedzic on 8/24


MMC (Mouse Move Counter)
By Laszlo Hegedüs on 8/24


Text-DB
By Jerome A. Simon on 8/24


JDos
By Jerome A. Simon on 8/23


Game 1945X
By Ozgun Harmanci on 8/23


Find hidden "back streamed" files on NTFS partitions. This code is a must for sec consultants.
By Israel B. Bentch on 8/22


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

using inline assemby - learn about registers, instructions, and a basic instruction list

Print
Email
 

Submitted on: 2/13/2001 11:50:38 AM
By: Jared Bruni  
Level: Intermediate
User Rating: By 8 Users
Compatibility:Microsoft Visual C++

Users have accessed this article 8540 times.
 

(About the author)
 
     Heres a tutorial which will teach you the basics so you can do some simple inline assembly. Contains code examples and compliments the examples which can also be found on PSC. Also contains x86 instruction list.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

inline assembly with Visual C++ tutorial

compliments the examples,

also contains simple instruction list

 

 

First off let me explain that this was a request from some email I got about

how this works

Anyway enough of my situation, here is the information:

using inline assembly

Within the C++ language is a keyword

_asm

{

}

Within the block contains the assembly mnemonics/instructions.

There not very hard to use, it usually contains a instruction followed by its operands. The operands usually contain registers, memory, memory addresses, and constant numbers. Normally in assembly language every line of code, assembles to one line of machine instructions. However this isnt always true, there are exceptions. A simple example of the use of a instruction is the following

mov eax,1

Now before we can truly really understand this instruction we have to understand a few other things which are important. Machine languages are priority languages, and therefore are designed to work for a specific type of chip. So they work in specific ways and in the x86 chip there are built in high speed memory locations called registers. These registers are used for data being passed in from memory, or other places to be manipulated, and then passed back out into memory or other places. Its kind of like you got to put the information in specific spots it can directly get to and then setup and call the instruction. What the above example means is mov into register eax the hex value 1. The comma separates the operands. Registers have different sizes, since the registers the cpu's evolved over time. The metaphor Jeff Duntermann makes in his book which I really recommend it taught me assembly quick and easily is " Assembly Language Step-Step", is " Adding a room to your house doesn't make it two houses--just one bigger house. And so it has been with the x86 registers." There are multiple types of registers some have specific tasks like the IP register (Instruction Pointer), Segment Registers, etc. The registers I am involved with in this example are just the general purpose registers which I am going to explain here The newer general purpose registers are much more general. In past days specific registers had certain agendas but these were actually limitations with the cpu. Now days the general purpose registers fall into three classes. The 32 bit general purpose registers, the 16 bit general purpose registers, and the 8 bit register halves. What should be noted however is that these registers are not really 3 distinct separate classes of information. These names really stand for regions of memory. How this is accomplished is done by a little naming system. The 16 bit registers and 8 bit register halves are really inside the 32 bit registers, and are just specific sections of memory. In a 32 bit register there are 32 bits. Look at the following graph:

ax

HI | ah al LOW

1111 1111 1111 1111 1111 1111 1111 1111 - register eax(full 32 bits)

The register eax is the full 32 bits of this memory location. The register ax is the lower half, or the low 16 bits. Register ah is the high half of the low 16 bits. al is the low 8 bits of ax (the low 16 bits). This can sound of tricky at first but soon as you understand the notation you can know how to pick out registers to use with your instructions. Its kind of you move stuff from places into the registers, and then use them in accordance with instructions, and then move the changes around to different places. Check out this notation example

General purpose Register eax

eax - full 32 bits

ax - low 16 bits

ah - high 8 bit register half

al - low 8 bit register half

ebx - full 32 bits

bx - low 16 bits

bh - high 8 bit register half

bl - low 8 bit register half

ecx - full 32 bits

cx - low 16 bits

ch - high 8 bit register half

cl - low 8 bit register half

edx - full 32 bits

dx - low 16 bits

dh - high 8 bit register half

dl - low 8 bit register half



Now that we understand the notion of the registers we an continue to use them with memory locations and other types of data. But what were going to explain here is just inline assembly. Since C++ hides most of its inner workings on how the actual code is outputted we can literally use C++ variables directly within your inline assembly. You can create functions that pass variables that can be manipulated with inline assembly, you can do all kinds of neat stuff. Here is a simple code snippet:

int __fastcall exampleadd(int x, int y)

{

int rt = 0;

_asm

{

mov eax,x

mov ebx,y

add eax,ebx

mov rt,eax

}

return rt;

}

 

This is a function which uses register based calling conventions which are pretty useful when you want just a little bit extra speed. Lets explain the code.

mov eax,x

Means move into register eax (the full 32 bit register) the value within the

variable x (VC when it compiles replaces this with the correct information).

mov ebx,y

Means move into register ebx (the full 32 bit register) the value within the

variable y (VC when it compiles replaces this with the correct information).

add eax,ebx

This means add the integer values in eax , with the values in ebx and put the values

back into eax.

mov rt,eax

Means move the value from register eax into the location where the variable

rt is at.

I hope I gave some light on this subject there's allot of really great tutorials

on the internet if you just look so yeah see ya.

- Jared

PS:

 

Here is a simple list of the instruction set I got from opcodes.txt

If your interested in understanding the instructions further I suggest

going and getting a guide to the instruction set.

8086/80186/80286/80386/80486 Instruction Set

AAA - Ascii Adjust for Addition

AAD - Ascii Adjust for Division

AAM - Ascii Adjust for Multiplication

AAS - Ascii Adjust for Subtraction

ADC - Add With Carry

ADD - Arithmetic Addition

AND - Logical And

ARPL - Adjusted Requested Privilege Level of Selector (286+ PM)

BOUND - Array Index Bound Check (80188+)

BSF - Bit Scan Forward (386+)

BSR - Bit Scan Reverse (386+)

BSWAP - Byte Swap (486+)

BT - Bit Test (386+)

BTC - Bit Test with Compliment (386+)

BTR - Bit Test with Reset (386+)

BTS - Bit Test and Set (386+)

CALL - Procedure Call

CBW - Convert Byte to Word

CDQ - Convert Double to Quad (386+)

CLC - Clear Carry

CLD - Clear Direction Flag

CLI - Clear Interrupt Flag (disable)

CLTS - Clear Task Switched Flag (286+ privileged)

CMC - Complement Carry Flag

CMP - Compare

CMPS - Compare String (Byte, Word or Doubleword)

CMPXCHG - Compare and Exchange

CWD - Convert Word to Doubleword

CWDE - Convert Word to Extended Doubleword (386+)

DAA - Decimal Adjust for Addition

DAS - Decimal Adjust for Subtraction

DEC - Decrement

DIV - Divide

ENTER - Make Stack Frame (80188+)

ESC - Escape

HLT - Halt CPU

IDIV - Signed Integer Division

IMUL - Signed Multiply

IN - Input Byte or Word From Port

INC - Increment

INS - Input String from Port (80188+)

INT - Interrupt

INTO - Interrupt on Overflow

INVD - Invalidate Cache (486+)

INVLPG - Invalidate Translation Look-Aside Buffer Entry (486+)

IRET/IRETD - Interrupt Return

Jxx - Jump Instructions Table

JCXZ/JECXZ - Jump if Register (E)CX is Zero

JMP - Unconditional Jump

LAHF - Load Register AH From Flags

LAR - Load Access Rights (286+ protected)

LDS - Load Pointer Using DS

LEA - Load Effective Address

LEAVE - Restore Stack for Procedure Exit (80188+)

LES - Load Pointer Using ES

LFS - Load Pointer Using FS (386+)

LGDT - Load Global Descriptor Table (286+ privileged)

LIDT - Load Interrupt Descriptor Table (286+ privileged)

LGS - Load Pointer Using GS (386+)

LLDT - Load Local Descriptor Table (286+ privileged)

LMSW - Load Machine Status Word (286+ privileged)

LOCK - Lock Bus

LODS - Load String (Byte, Word or Double)

LOOP - Decrement CX and Loop if CX Not Zero

LOOPE/LOOPZ - Loop While Equal / Loop While Zero

LOOPNZ/LOOPNE - Loop While Not Zero / Loop While Not Equal

LSL - Load Segment Limit (286+ protected)

LSS - Load Pointer Using SS (386+)

LTR - Load Task Register (286+ privileged)

MOV - Move Byte or Word

MOVS - Move String (Byte or Word)

MOVSX - Move with Sign Extend (386+)

MOVZX - Move with Zero Extend (386+)

MUL - Unsigned Multiply

NEG - Two's Complement Negation

NOP - No Operation (90h)

NOT - One's Compliment Negation (Logical NOT)

OR - Inclusive Logical OR

OUT - Output Data to Port

OUTS - Output String to Port (80188+)

POP - Pop Word off Stack

POPA/POPAD - Pop All Registers onto Stack (80188+)

POPF/POPFD - Pop Flags off Stack

PUSH - Push Word onto Stack

PUSHA/PUSHAD - Push All Registers onto Stack (80188+)

PUSHF/PUSHFD - Push Flags onto Stack

RCL - Rotate Through Carry Left

RCR - Rotate Through Carry Right

REP - Repeat String Operation

REPE/REPZ - Repeat Equal / Repeat Zero

REPNE/REPNZ - Repeat Not Equal / Repeat Not Zero

RET/RETF - Return From Procedure

ROL - Rotate Left

ROR - Rotate Right

SAHF - Store AH Register into FLAGS

SAL/SHL - Shift Arithmetic Left / Shift Logical Left

SAR - Shift Arithmetic Right

SBB - Subtract with Borrow/Carry

SCAS - Scan String (Byte, Word or Doubleword)

SETAE/SETNB - Set if Above or Equal / Set if Not Below (386+)

SETB/SETNAE - Set if Below / Set if Not Above or Equal (386+)

SETBE/SETNA - Set if Below or Equal / Set if Not Above (386+)

SETE/SETZ - Set if Equal / Set if Zero (386+)

SETNE/SETNZ - Set if Not Equal / Set if Not Zero (386+)

SETL/SETNGE - Set if Less / Set if Not Greater or Equal (386+)

SETGE/SETNL - Set if Greater or Equal / Set if Not Less (386+)

SETLE/SETNG - Set if Less or Equal / Set if Not greater or Equal

SETG/SETNLE - Set if Greater / Set if Not Less or Equal (386+)

SETS - Set if Signed (386+)

SETNS - Set if Not Signed (386+)

SETC - Set if Carry (386+)

SETNC - Set if Not Carry (386+)

SETO - Set if Overflow (386+)

SETNO - Set if Not Overflow (386+)

SETP/SETPE - Set if Parity / Set if Parity Even (386+)

SETNP/SETPO - Set if No Parity / Set if Parity Odd (386+)

SGDT - Store Global Descriptor Table (286+ privileged)

SIDT - Store Interrupt Descriptor Table (286+ privileged)

SHL - Shift Logical Left

SHR - Shift Logical Right

SHLD/SHRD - Double Precision Shift (386+)

SLDT - Store Local Descriptor Table (286+ privileged)

SMSW - Store Machine Status Word (286+ privileged)

STC - Set Carry

STD - Set Direction Flag

STI - Set Interrupt Flag (Enable Interrupts)

STOS - Store String (Byte, Word or Doubleword)

STR - Store Task Register (286+ privileged)

SUB - Subtract

TEST - Test For Bit Pattern

VERR - Verify Read (286+ protected)

VERW - Verify Write (286+ protected)

WAIT/FWAIT - Event Wait

WBINVD - Write-Back and Invalidate Cache (486+)

XCHG - Exchange

XLAT/XLATB - Translate

XOR - Exclusive OR


Other 273 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
2/13/2001 2:27:39 PM:luke webster
Excelent. Great tutorial, I really found it useful, now that i know alittle bit of ASM i made a simple calculator that can add ,subtract, divide, and multiply using ASM. I've been wanting to do this for awhile now...Thanks Jared :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/13/2001 10:23:20 PM:luke webster
By the way, If Jared or anyone else knows where i could get some Inline ASM Tutorials please leave the link below or email me because I've been looking and looking for them but all i can find is inline asm using pascal :(
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/14/2001 7:15:06 AM:Dean lancaster
If you want some REALLY good tutorials, albeight in broken pidgeon english like this one then check out warez sites. ware.at/tnt is a good place to start.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/15/2001 1:16:27 AM:Jared Bruni
heres a web page with a 32 bit assembler for windows and a whole section of tutorials. http://www.piic.net/~win32asm/ masm 32 it works good for learning it
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/15/2001 1:51:17 AM:michael
hey this tutorial is really good, makes assembly seem a little less scary to beginers, registers are usually a little harder to understand, especially after reading some of the tutorials on the internet...people take for granted that you know too much already
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/15/2001 1:52:16 AM:Michael
hey this tutorial is really good, makes assembly seem a little less scary to beginers, registers are usually a little harder to understand, especially after reading some of the tutorials on the internet...people take for granted that you know too much already
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/17/2001 3:36:03 AM:Jake
Excellent tutorial! I always wanted to know about this. Thanx, thats 5 globes from me! :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/26/2001 1:12:22 AM:Jared
new link for the masm32 is www.masm32.cjb.net or www.win3 2asm.cjb.net
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/13/2001 1:11:52 AM:KM Leow
Can you show me how to send instructions to CPU from VB6? Please send to kmleow@bigfoot.com Thanks.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/20/2001 2:02:28 PM:Drew Campbell
I am trying to use asm code within VC++ on a WIN NT platform. It allows me to use some asm code, but whenever I try to run an Interupt, it returns an error: The instruction at
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/20/2001 4:23:34 PM:Jared Bruni
I get the same type of error when I try to use interupt
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/13/2001 7:28:22 AM:Polaris
hi people i would like to have help about using inline ASM for a function in fact i would like to write a ASM routines that would be able to manipulate an array of structure Data for image processing image that Function InvertRGBarray(RGB *Myarray,short Width,short Height) { for (int X=0;X<=(width*height)-2;X++) RGB Val; Val=*Myarray; Val.R=255-Val.R; Val.G=255-Val.G; Val.B=255-Val.B; *Myarray=Val; *Myarra y++; } } I have tried to use ESI register and add ESI 2 bytes by my program crash Please Help Or if someone knows great algorytms for doing great image processing method Let me know at Johna.pop@caramail.com
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/26/2002 11:55:30 PM:binu
sir i have an exe file in borland c++. i disassembled it using wdasm tool.is there any tool to convert the disassembled 80486 code back to borland c++ code ? kindly inform me with regards binu
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.