Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Assembly Memory Variables Allocating Memory We have already seen that each variable that you wish to use in your program must be declared in the DATA segment (i.e. after the line .data). If you are going to reference it in the CODE segment (i.e. after the line .code) , then at the start of the code segment the two lines: mov ax, @data mov ds, ax must be used to tell the compiler where the start of the data segment is and thus the variables can be successfully located when required in the code segment. The mnemonic dw is not an 8086 instruction. It is an assembler directive, instructing the assembler to define a word in memory with the initial value of 0. The type of variable is associated with its size, a byte is one byte in size, a word is two bytes and a double word is four bytes. The assembler directives to define a byte, word and double word are db, dw and dd. The following short section of a program illustrates this idea (the full program is on glasnost): ; Example of memory allocation to variables ; Authors: RC/RJ Class: CNOC1 Date: March 1998 title prog10.asm .model small .stack 100h .data inchar db 0 innumber dw 0 .code mov ax, @data ; put address of data segment into ax mov ds, ax ; put address of data segment into ds . . mov ah, 1 int 21h ; inputs a character into al mov inchar, al ; stores the char in al into memory call indec ; input a number, store in ax mov innumber, ax ; move the number to memory . . mov ah, 4ch int 21h ; terminate program end 4/29/2017 Lect1198.doc - Memory Allocation Page: 1 Assembly Strings We also have seen memory allocation for strings, a string is treated as a sequence of ASCII characters, which are declared in the same way as above. .data string ‘This is an example string’, 13,10,’$’ db This initialises consecutive bytes in memory to this ASCII string. The $ is called the string delimiter, this is needed when we use DOS function call to print out the string, as DOS will print from the specified memory location each consecutive byte until it encounters a $, at which point it stops. Blocks of storage Sometimes we wish to declare block of storage for arrays or for buffers. This can be done as follows: .data numarray Name of variable dw 10,20,30,40,50 Type of data List of initial elements declares an array of 5 words (i.e. integers) called “numarray”, and the initial value of each of the words are given in the list. This method can be quite time consuming when we have a large array to declare. Also, we are not usually concerned with what the initial content of this memory is. An easier way to declare a block of memory is to use the dup, the duplicated clause, for example: .data numarray Name of variable Type of data dw 100 Number of elements dup(?) Copy that number Random initial values This declares an array of 100 words (i.e. integers), and does not initialise the words. To initialise all the words, say to 0, then we would say: .data numarray dw 100 dup(0) All initial values are zero. 4/29/2017 Lect1198.doc - Memory Allocation Page: 2 Assembly To declare an array of 1000 bytes, say for a character array, one would write: .data chararray db 1000 dup(0) Access to data in a block of storage (i.e. in an array) : There are a few ways to access the data in a block of memory (i.e. in an array), but having used a high level language like Turbo-C the simplest method to start with is using the name of the variable and the array notation [ ], as in high level langauges, where the particular element in the array is stored in either the source register si or the destination register di. For example, to read in 10 numbers into an array called “numlist” : .data numlist dw 10 dup(0) .code indata: . . mov ax, @data mov ds, ax mov cx, 10 mov si, 0 call indec mov numlist[si], ax add si, 2 loop indata . . ; sets up data segment register ds ; initialise the counter to 10 ; initialise the array pointer, use si or di ; Read in an integer ; Store the inputted number in the array ; Move the pointer the the next integer ; Read in more numbers if cx > 0 Prog11.asm on the common drive uses the above method to read in 10 integers, store them in an array and then output them in reverse order. Points to note: 1. We see that in this program we are using one of the index registers, si , as a “pointer” into the array, and since this array is an array of words, this pointer has to be updated by 2 each time. This pointer specifies the byte offset of the required element from the start of the array. 2. When pointing to an array, one should use an index register, i.e. either si or di 4/29/2017 Lect1198.doc - Memory Allocation Page: 3 Assembly A second way to access the data in the array above is to point a base pointer (using normally bp register or possibly the bx register) to the start of the array, as for example: mov bp, offset numlist ; The address of the start of array to bp Now to read in an integer use: call mov indec [bp+si] , ax ; Read in the integer ; Put the number in the array at position si bytes ; after the start of the array Prog11a.asm on the common drive uses this method to access the data elements in the array. Points to note: 1. There is an assembly instruction lea ( load effective address ) which is often used instead of the offset instruction above. So the line: mov bp, offset numlist is the same as: lea bp, numlist ; load address of numlist into bp 2. In prog11.c and prog11a.c when the data has been read into the array the pointer si to the elements in the array has gone 2 bytes too far, so to read back the numbers in reverse order the following code segment is used: next : sub mov mov call sub loop si, 2 ; Go to last integer entered cx, 10 ; Reset counter to 10 again ax, numlist[si] ; Put next intager into ax outdec ; output the integer si, 2 ; Move the pointer back one more integer next ; If cx > 0 , go back for next integer . 4/29/2017 Lect1198.doc - Memory Allocation Page: 4