Unformatted text preview:

Intro to Assembler 8/24/2010COMP375 1MoreMoreAssembler LanguageAssembler LanguageCOMP375Computer Architecture and OrganizationAssembler Programming“A programming language is low level h it i tt ti t thwhen its programs require attention to the irrelevant.”- Alan J. Perlis “You can do everything in assembler, but no one wants to program in assembler pganymore.” - Yukihiro MatsumotoGoals for Today• Introduce assembler language constructs to:–Compute integer equations– If statements– loops•Write simple assembler programs•Write simple assembler programs.Intel Registers• The Intel Pentium has eight 32-bit general-purpose registerspp gIntro to Assembler 8/24/2010COMP375 2mov Instruction• The mov instruction moves data between ditbttmemory and a register or between two registers.• The format is mov destination, sourcewhere destination and source can be•where destination and source can be– register, memory to load data into a register– memory, register to store data into memory– register, register to move data between regsConstants• Assembler programs can also use small constantsconstants.• If you want to move the number 47 to eaxmov eax,47• Constants can also be charactersmoval‘Z’moval,Z• You can also move constants to memorymov aardvark,15Memory Model• Memory is a huge one dimensional array fb tof bytes.• Both data and instructions are stored in memory.• Registers can hold copies of the data or the address of the data in memorythe address of the data in memory.Hardware Data Types• The hardware provides only a few primitive data typesdata types– long, int and short (8, 4 & 2 bytes) – float and double (4 & 8 bytes)– char or byte (1 byte)•Integer data types can be signed or gyp gunsignedIntro to Assembler 8/24/2010COMP375 3Software Data Types• All other data types are created by ftsoftware• strings• objects• boolean• multi-dimensional arraysArithmetic• All arithmetic and logical functions (AND, OR XOR t ) t b d i thOR, XOR, etc.) appear to be done in the registers.• Each instruction has one operand in a register and the other in memory or another register.another register.add eax, dog• The result is saved in the first register.Arithmetic and Logical Instructionsmnemonic operationADDAddADDAddSUB SubtractMUL Unsigned MultiplyIMUL Signed MultiplyDIVUnsigned DivideDIVUnsigned DivideIDIV Signed DivideAND Logical ANDOR Logical ORArithmetic Exampleint dog=3, cat=4, bird=5;_asm{// bird = dog + cat;mov eax,dogadd eax,catmov bird,eax}Intro to Assembler 8/24/2010COMP375 4Arithmetic Example 2int dog=3, cat=4, bird=5, cow;_asm{ // cow = dog + cat - bird;mov eax,dogadd eax,catsub eax,birdmov cow,eax}asm1-2What value is in EAX at the end?int dog=4, cat=3, bird=5;_asm{_mov eax,dogsub eax,catmov bird,eax}1. 1222.23. 34. 45. 5Big Operands• Multiplication and Division use two i t t t 64 bit lregisters to store a 64 bit value.• A number is stored in EDX:EAX with the most significant bits in the EDX register and the least significant bits in EAX. EDXEAXEDXEAXbits 63, 62, … 33, 32 bits 31, 30 … 2, 1, 0Multiplication• The imul signed multiply instruction has three forms.• Multiply memory * EAX, save in EDX:EAXimul memorreg• Multiply memory * register, save in registerimulregmemregimulreg, memorregIntro to Assembler 8/24/2010COMP375 5Division• The 64 bit number in the EDX:EAX pair of registers is divided by the 32 bit value in aregisters is divided by the 32 bit value in a memory location or another register.• The resulting quotient is stored in EAX• The resulting remainder is stored in EDX• Since the EDX:EAX registers are always gyused, you do not have to specify them.idiv memoryAddrArithmetic Example 2int dog=9, cat=4, bird=5, cow;_asm{// cow = dog * cat / bird;mov eax,dog ; move dog to eax regimul cat ; edx:eax = dog*catidiv bird ; eax = dog*cat/birdmov cow,eax ; save result in cow}Arithmetic Example 3int dog=9, cat=4, bird=5, cow;_asm{ // cow = dog % cat + bird;mov eax,dog ; move dog to eax regmov edx,0 ; clear EDXidiv cat ; edx = dog % catadd edx,bird ; add bird to dog % catmov cow,edx ; save result in cow}Shifting Bits• You can move the bits in a register right or left.left.00000101 Left shift 2 0001010011111011 Right shift 1 0111110111111011Arith R shift 111111101Intro to Assembler 8/24/2010COMP375 6Shifts• The SHR and SHL instructions shift the bits right or left by the specified number of bits. • The SAR and SAL instructions shift the bit right or left, but not the sign bit. The SAR copies the sign bit into the emptied bits.• The shift count can be a constant or a number in theclregisternumber in the clregistersar eax, 5shl eax, clWhy Shift?• Sometimes you need to separate some bits from a word• Example: separate bits 2 – 5 of an intmov eax, someint; get word1 1 1 0 1 1 0 1,;ge wodshr eax, 2 ; shift desired bits to endand eax, 15 ; move other bitsmov results, eax ; save resultsShift Arithmetic• Shifting a number to the left multiplies it by 2 for each bit you shift.• Shifting a number to the right divides it by 2 for each bit you shift.Shift Exampleint dog=3;_asm {mov eax,dog ; eax = 3sal eax,2 ; eax = 12sar eax,1 ; eax = 6}Intro to Assembler 8/24/2010COMP375 7What is the result after shr ax,5 when the initial value of ax is 11000000011000011. 11111110000000112. 00000110000000113. 00001100001000004. 1100000001100001Try It• Complete this program to compute the average of two numbersint bull, dog, goat, two=2;cin >> bull >> dog;_asm{// set goat to the average of dog and bull}cout << goat << endl;Possible Solutionint bull, dog, goat, two=2;cin >> bull >> dog;_asm{mov eax, bull ; eax = bulladd eax, dog ; eax = bull + dog mov edx, 0 ; clear for divideidiv two ;divide by 2 mov goat, eax ; save result}cout << goat << endl;Another Possible Solutionint bull, dog, goat;cin >> bull >> dog;_asm{mov eax, bull ; eax = bulladd eax, dog ; eax = bull + dog sar eax, 1 ; divide by 2 mov goat, eax ; save result}cout << goat << endl;Intro to Assembler 8/24/2010COMP375 8Increment and Decrement• The inc and dec instructions are one of th f th t l tithe few that can run on memory locations without using the registers.• You can increment or decrement the value in a register for memory locationinc eaxdec memoryAddrIntel Status Register• The status register records the results of ti th i t tiexecuting the instruction.• Performing arithmetic sets the status register.• The


View Full Document

NCA&T COMP 375 - More Assembler Language

Download More Assembler Language
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view More Assembler Language and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view More Assembler Language 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?