How to resolve the algorithm Pointers and references step by step in the 8086 Assembly programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Pointers and references step by step in the 8086 Assembly programming language
Table of Contents
Problem Statement
In this task, the goal is to demonstrate common operations on pointers and references. These examples show pointer operations on the stack, which can be dangerous and is rarely done. Pointers and references are commonly used along with Memory allocation on the heap.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Pointers and references step by step in the 8086 Assembly programming language
Source code in the 8086 programming language
.model small
.stack 1024
.data ; data segment begins here
UserRam byte 256 dup (0) ; the next 256 bytes have a value of zero. The address of the 0th of these bytes can be referenced as "UserRam"
tempByte equ UserRam ;this variable's address is the same as that of the 0th byte of UserRam
tempWord equ UserRam+2 ;this variable's address is the address of UserRam + 2
.code
start:
mov ax, @data
mov ds, ax
mov ax, @code
mov es, ax
mov ax,0FFFFh ;see note 1
mov word ptr [ds:tempWord],ax ;store hexadecimal value FFFF into tempWord
mov bl, 80h
mov byte ptr [ds:tempByte],bl ;the register size needs to match the data type
;Note 1:
;UASM doesn't like leading hex digits A-F so a 0 is placed in front.
;It doesn't change the storage type of the operand. (i.e. this is still a 16 bit value even though there are 5 digits)
mov ax, byte ptr [ds:tempByte] ;assembler will generate an error - operands don't match
mov al, word ptr [ds:tempWord] ;assembler will generate an error - operands don't match
mov ax, word ptr [ds:tempByte] ;assembler will allow this even though the intended data size is wrong
mov al, byte ptr [ds:tempWord] ;assembler will allow this even though the intended data size is wrong
LDS bx,UserRamPtr ;loads [ds:bx] with UserRamPtr
.data
myString db "Hello World!",0 ;the zero is the null terminator
.code
mov bx, seg myString ;load into bx the segment where myString is stored.
mov ds, bx ;load this segment into the data segment register. On the 8086, segment registers can't be loaded directly.
mov bx, offset MyString ;the memory address of the beginning of myString. The "H" is stored here.
add bx, 2 ;add 2 to bx. bx contains the memory address of the first "l" in "Hello"
mov al,[ds:bx] ;dereference the pointer and store the value it points to into al.
You may also check:How to resolve the algorithm Flow-control structures step by step in the AWK programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the REXX programming language
You may also check:How to resolve the algorithm Almkvist-Giullera formula for pi step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Binary digits step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm Left factorials step by step in the Clojure programming language