Presentation is loading. Please wait.

Presentation is loading. Please wait.

Converting a Negative Decimal Number to the 2’s Complement Representation : Represent (-18) 10 as a signed 8-bit binary number Solution: Abs(-18) 10 :0001.

Similar presentations


Presentation on theme: "Converting a Negative Decimal Number to the 2’s Complement Representation : Represent (-18) 10 as a signed 8-bit binary number Solution: Abs(-18) 10 :0001."— Presentation transcript:

1 Converting a Negative Decimal Number to the 2’s Complement Representation : Represent (-18) 10 as a signed 8-bit binary number Solution: Abs(-18) 10 :0001 0010 1’s Complement:1110 1101 Add 1: + 0000 0001 Result:1110 1110 Therefore, (-18) 10 = (1110 1110) 2 1

2 Converting a Negative 2’s Complement Number to Decimal Representation: Example: Represent (1001 1000) 2 as a signed decimal number Since MSB = 1 then the number is negative Use signed conversion process Solution: The number:1001 1000 1’s Complement:0110 0111 Add 1: + 0000 0001 Result:0110 1000 Decimal 104 -ve sign -104 Therefore(1001 1000) 2 = (-104) 10

3 Addition of 8-bit (positive and negative) signed number: Perform the addition: 8 + (-17) Find the 2’s complement representation (8) 10 = 0000 1000 (-17) 10 = ??? (17) 2 0001 0001 1’s complement:1110 1110 Add 1 +0000 0001 Result1110 1111 Therefore (-17) 10 = (1110 1111) 2

4 Now (8) 10 0000 1000 (-17) 10 +1110 1111 Result(1111 0111) 2 = (?) 10 Convert the result to decimal representation: 1111 0111 take 1’s complement 0000 1000 Add 1 +0000 0001 Result(0000 1001) 2 = (+9) 10 Therefore, (1111 0111) 2 is -9 in decimal

5 There are eight general-purpose registers, six segment registers, a register that holds processor status flags (EFLAGS), and an instruction pointer (EIP).

6 EAX ( Accumulator) is automatically used by multiplication and division instructions. It is often called the extended accumulator register for accumulating operands and results. EBP ( Base) is used by high-level languages to reference function parameters and local variables on the stack (data on the stack). It should not be used for ordinary arithmetic or data transfer except at an advanced level of programming. It is often called the extended frame pointer register. ECX ( Count) used as counter for string and loop operations. EDX (Data) register that can be used for I/O pointer. ESP stack pointer register. ESI and EDI are used by high-speed memory transfer instructions. They are sometimes called the extended source index and extended destination index registers.

7 Status Flags The Status flags reflect the outcomes of arithmetic and logical operations performed by the CPU. They are the Overflow, Sign, Zero, Auxiliary Carry, Parity, and Carry flags. Their abbreviations are shown immediately after their names. The Carry flag (CF) is set when the result of an unsigned arithmetic operation is too large to fit into the destination. The Overflow flag (OF) is set when the result of a signed arithmetic operation is too wide (too many bits) to fit into the destination. The Sign flag (SF) is set when the result of an arithmetic or logical operation generates a negative result.

8 The Zero flag (ZF) is set when the result of an arithmetic or logical operation generates a result of zero. The Auxiliary Carry flag is set when an arithmetic operation causes a carry from bit 3 to bit 4 in an 8-bit operand. The Parity flag sums the number of bits that are set in a number, and indicates whether the sum is odd or even.

9 Reserved Words Assembly language has a list of words called reserved words. These have special meaning and can be used in their correct context. Reserved words can be any of the following: Instruction mnemonics, such as MOV, ADD, or MUL, which correspond to built-in operations preformed by Intel processor, Directives, which tell MAZM how to assemble programs. Attributes, which provide size and usage information for variables and operands. Examples are BYTE and WORD. Operators, used in constant expressions. A complete list of MAZM reserved words will be found in Appendix D.

10 Identifiers An identifier is a programmer chosen name. it might identify a variable, a constant, a procedure, or a code label. Keep the following in mind when creating identifiers: They may contain between 1 and 247 characters. They are not case sensitive. The first character must be either a letter (A…Z, a…z ), underscore(_), $. Subsequent character may also be digits. An identifier can not be the same as an assembler reserved word. Example of identifiers Var1count$first Maxopen

11 Directive A directive is a command that is recognized and acted upon by the assembler as the program's source cod is being assembled. Directives are being used for defining logical segments, choosing a model, defining variables, creating procedures, and so on. Different capitalization of the same directive are assumed to be equivalent. For example the assembler does not recognize any difference between.data,.DATA, and.Data. Examples of directives are:.Data ‘identifies the area of a program that contains variables’.code ‘ identifies the area of a program that contains instructions’ A-name proc‘identifies the beginning of procedures It would take a very long time to learn all the directives in MAZM, so we concentrate on the few that are most essential.

12 Comments Comments as you probably know, are an important way for the writer of a program to communicate information about how the program works to the person reading the source code. Comments can be specified by semicolon. Example: ;This line is coment; For Block comments you can use COMMENT directive and a user specified symbol. Example: COMMENT & This line is comment this line is also comment &

13 Program Template TITEL Program Template ; Program Description: ; Author: ; Creation Date: ; Revisions: ; Date:Modified by: INCLUDE Irvine32.inc.data ; ( insert variables here ).code main PROC ; (insert executable instructions here) exit main ENDP ; ( insert additional procedure here) END main

14 Example: TITLE Hello Program ; Program Description: This program will display Hello World message on the ; screen ; Author: Sahar Mosleh ; Creation Date: August 30, 2005 INCLUDE Irvine32.inc.data ; (insert variables here) prompt1byte “Hello World”,0 ; Store the prompt1 in ; memory.code main PROC ; (insert executable instructions here) callclrscr; clear the screen movedx, offset prompt1; move the address of prompt to edx callwritestring; display the string that is stored at ;the address pointed by edx callcrlf; move the courser to next line exit main ENDP ; (insert additional procedure here) END main

15 Example: TITLE Print Integer ; Program Description: This program will display integer number 216543 on the ; screen ; Author: Sahar Mosleh ; Creation Date: August 30, 2005 INCLUDE Irvine32.inc.data ; (insert variables here).code main PROC ; (insert executable instructions here) callclrscr; clear the screen moveax, 2156543; move the integer to eax callwriteInt; display the INT that is stored at eax callcrlf; move the courser to next line exit main ENDP ; (insert additional procedure here) END main

16 Example: TITLE Input Integer ; Program Description: This program will read integer from user and out put ;it on the screen ; Author: Sahar Mosleh ; Creation Date: August 30, 2005 INCLUDE Irvine32.inc.data ; (insert variables here) prompt1byte “Please input an intege”,0 ; Store the prompt1 in ; memory.code main PROC movedx, offset prompt1; move the address of prompt to edx callwritestring; display the string that is stored at ;the address pointed by edx callcrlf; move the courser to next line callReadint;read the int from user and put it in eax callwriteint;display the content of eax exit main ENDP ; (insert additional procedure here) END main

17 LOOP Instruction The LOOP instruction provides a simple way to repeat a block of statements a specific number of times. ECX is automatically used as a counter and is decremented each time the loop repeats. The Loop instruction involves two steps: First, it subtracts 1 from ECX. Next it compares ECX to zero. If ECX is not equal to zero, a jump is taken to the label identified instruction following the loop.

18 Example: In the following example, we add 1 to EAX each time the loop repeats. When the loop ends, EAX = 5 and ECX = 0 moveeax,0 moveecx5 L1: Inceax LoopL1 A common programming error is to inadvertently initialize ecx to zero before beginning of the a loop. If this happens, the Loop instruction decrements ECX to FFFFFFFFh, and the loop repeats 4,294,967,296 times.

19 Nested loops When You must create a loop inside another loop, the problem arises of what to do with the counter in ECX. Saving the counter loop count in a variable is a good solution:.data count Dword ?.code Mov ecx,100;set outer loop counter L1: Mov count,ecx;save outer loop counter Mov ecx,20;set inner loop counter L2: : Loop L2;repeat the inner loop count Mov ecx, count;restore outer loop count loopL1;repeat the outer loop

20 Direct memory operand Example:.data Var1Dword100h :.code Moveaxvar1

21 Indirect operand An indirect operand can be any 32-bit general purpose register ( EAX,EBX,ECX,EDX,ESI,EDI,EBP, and ESP) surrounded by brackets. The register is assumed to contain the offset of some data. For example ESI contains the offset of variable 1:.data Val1byte10 h.code Movesi, offset val1 If a move instruction uses the indirect operand as the source, the pointer in ESI is dereferenced and a byte is moved to EAX MovEAX[esi]Eax = 10 h Or if the indirect operand is the destination operand, a new value is placed in memory at the location pointed to by the register: Mov[esi]EBX

22 Array Indirect operands are practically useful when dealing with arrays because an indirect operand’s value can easily be modified. Similar to an array subscript, an indirect operand can point to different array elements. For example, ArrayB contain three bytes. We can increment ESI and make it to point each byte, in order:.data ArrayB Byte10h, 20h, 30h.code Mov esi, offset ArrayB Mova1, [esi] incesi Mova1, [esi] Incesi Mova1, [esi]

23 MASM has a number of operators that are effective tools for describing and addressing variables: The Offset operator returns the distance of a variable from the beginning of it’s enclosing segment The DUP operator generates a repeated storage allocation. The TYPE operator returns the size ( in bytes ) of each element in an array LENGHTOF operator returns the number of elements in an array The SIZEOF operator returns the number of bytes used by an array initializer. These operators are only a small subset of the operators supported by MASM. You may want to view the complete list in Appendix D.

24 Example: ArrayBByte10h, 20h, 30h, 40h ArrayDWDword1000,2000,3000,4000 The pointers to the above arrays can be define as: ptrBDwordArrayB PtrDWDwordArrayDW

25 Example: TITLEpointers Include Irvine32.inc.data arrayBByte10h,20h,30h arrayDDword4,5,6 Ptr1DwordarrayB;pointer to arrayB Ptr2DwordarrayD;pointer to arrayD.code mainproc movesi, ptr1 ;move the address (pointer to arrayB) to esi mova1 [esi] movesi, ptr2 ;move the address (pointer to arrayD) to esi moveax,[esi] Exit MainENDP ENDmain

26 .data Buffer Byte 50 DUP(0);holds the characters byteCountDword ?;holds counter for loop.code movedx, offset buffer;points to the buffer movecx, (sizeof buffer)-1;specify max characters call ReadString;input the string mov bytecount, eax

27 The These color constants are defined in Irvine32.inc. The backgroung color must be multiplied by 16 before being added to the foreground color. The following for example, defines yellow characters on a blue background Yellow + (blue * 16) Before calling SetTextColor, move the desired color to EAX Moveax, white + (blue*16) Call setTextColor

28 Push operation A 32-bit push operation decrements the stack pointer by 4 and copies a value into the location in the stack pointed to by the stack pointer. In the following figure, we push 0000005A on the stack: Before the push, ESP=00001000h, and after the push, ESP=000000FFCh. ESP

29 Pop Operation A pop operation removes a value from the stack and places it in a register or variable. After the value is popped from the stack, the stack pointer is incremented to point to the next highest location in the stack. The following diagram shows the stack before and after the value 00000002 is popped from the stack: 00001000 00000FFC 00000FF8 00000FF4 00000FF0

30 PUSH and PUP Instructions PUSH Instruction: The PUSH instruction first decrements ESP and then copies either a 16 or 32 bit source operand into the stack. A 16 bit operation causes the ESP to be decremented by 2. A 32-bit operand causes ESP to be decremented by 4. There are Three instruction format PUSHr/m16 Pushr/m32 PUSHimm32 If your program calls procedures from the Irvine 32 library, you should always push 32-bit values

31 Pop Instruction The pop instruction first copies the contents of the stack element pointed to by ESP into a 16- or 32-bit destination operand and then increments ESP. If the operand is 16 bits, ESP is incremented by 2. if the operand 32 bits, ESP is incremented by 4. POPr/m16 POPr/m32

32 When you create a procedures other than your program's main procedure, end it with a RET instruction. It forces the CPU to return to the location from the procedure was called: SumOf PROC addeax,ebx addeax,ecx; Return the Sum value in EAX Ret SumOf ENDP The startup procedure ( main Procedure) is a special case because it ends with the exit statement. When you INCLUDE Irvine 32.inc statement, exit Is an alias for a call EXITProcess, a MS_Windows function call that terminates the program.

33 Call and ret instructions The CALL instruction call a procedures by directing the processor to begin execution at a new memory location. The call instruction pushes its return address on the stack and copies the called procedure’s address into the instruction pointer (EIP) When the procedures is ready it uses RET instruction to bring the Processor back to the point in the program where the procedure was called. By returning the address form stack to instruction pointer register EIP The CPU always executes the instruction in memory pointed to by EIP

34 When the call instruction executes, The address following the call 00000025 is pushed on the stack and the address of MySub is located into as shown here: Main proc 00000020 Call MySub 00000025 Movebx,eax All the instruction in MySub execute up to its RET instruction. When the RET instruction executes, the value in the stack pointed by ESP is popped into EIP. 00000040MySubproc Moveax, edx : Ret MySubENDP

35 .data Array Dword10000h, 20000h, 30000h, 40000h, 500000h theSumdowrd? MainPROC movesi, offset array; ESI pointes to array movecx,lenghtof Array ; ECX = array count callArraySum ; call calculate the sum movtheSum,eax ; returned sum in EAX callwriteint ; print the sum exit mainENDP

36 ;-------------------------------------------------------------------- ArraySumPROC ; ;calculates and returns the array of 32-bit integers. ; recieives: ESI = Array ; ECX= number of elements in the array ; Returnes: EAX=Sum ;------------------------------------------------------------------- pushesi; Save ESI and ECX push ecx moveax,0; Set Sum to Zero L1: addeax,[esi]; add each integer to sum addesi,4; point to next integer loopL1; repeat for array size Popecx: restore ecx and ESI Popesi ret; Return sum in EAX ArraySum ENDP End mian

37 MLU Operations The MUL instruction multiplies an 8-,16-,32-bits operand by either AL,AX,or EAX. The instruction format is: MULr/m8 MULr/m16 MULr/m32 The result is in EDX and EAX. EDX sets the flag register if it is not zero ( as the result of operation over flow situation is Eax carries the product of operation. Will we go through the detail of multiplication and division instruction later on this course.

38 Example: TITLE Multiply (AddSub2.asm) ; This program Multiply two decimal numbers together. We need to put the first operand in eax, the ;second operand in another multi purpose register. The product goes to eax ; Last update: 2/1/02 INCLUDE Irvine32.inc.data val1 dword 10 val2 dword 40.code main PROC moveax,val1; eax=10 movebx,val2; ebx=40 mulebx; eax=ebx*eax callwritedec exit main ENDP END main


Download ppt "Converting a Negative Decimal Number to the 2’s Complement Representation : Represent (-18) 10 as a signed 8-bit binary number Solution: Abs(-18) 10 :0001."

Similar presentations


Ads by Google