180 likes | 309 Views
Practical2. Assembly Programming. Initialising Variables (Recap). Initialising variables are done in the .data section .code val1 dd 10 val2 dd 20. Initialising Variables (Recap). Val1. dd. 10. Variable Name. Variable Type. Value. Addition. This is done in the .code section
E N D
Practical2 Assembly Programming
Initialising Variables (Recap) • Initialising variables are done in the .data section .code val1dd 10 val2dd 20
Initialising Variables (Recap) Val1 dd 10 Variable Name Variable Type Value
Addition • This is done in the .code section moveax, val1 add eax, val2 InvokeWriteInt
Addition Explanation moveax, val1 • This code moves val1 to the eax register add eax, val2 • This adds val2 to the eax which contains val1 (val1+val2) Invoke WriteInt • Writes the answer to the screan
Subtraction • Similar to addition. • Replace add with sub moveax, val1 subeax, val2 InvokeWriteInt
Multiplication • This is done in the .code section moveax, val1 mulval2 InvokeWriteInt
Multiplication Explanation moveax, val1 Mulval2 • This multiplies eax with val2 • Note the difference for multiplication Invoke WriteInt • Writes the answer to the screan
Division • This is done in the .code section moveax, val1 movebx, val2; stores val2 in ebx subedx, edx divebx
Division Explanation moveax, val1 movebx, val2 Moves val2 into ebx subedx, edx divebx Divides eax by ebx
Division By Negative • If you want to divide by a negative number you use idiv
The Read Function • ReadStringis used to read in strings • ReadInt is used to read in values
ReadString • In the .data section you create • unamedb 50 dup(0) • This creates the empty string uname
ReadString • In the .code section to call uname movedx, offset uname • point dx, to address of input uname invokeReadstring • read input as string into uname
ReadInt • In the .data section you create anumberdd? • This creates the empty intanumber • Don’t forget the question mark (?)
ReadInt invoke Readint Reads in the integer from command prompt movanumber, eax copy the value from command prompt into the variable anumber
* Hint for Prac 2 • A possible solution to the string reverse problem is labels for a loop that pushes a stack and pops it (will reverse the string). • Here is an example of pushing a stack and looping using a label. Similarly you need to pop the stack by reusing this code (creating label L2. On the next slide.
* Hint for Prac 2 • movecx,nameSize ; Push the name on the stack. • mov esi,0 • L1: movzxeax,source[esi] ; get character • push eax ; push on stack • incesi • Loop L1