ABCDEFG Diamond on 8086 Assembly
Making a Diamond pattern using alphabet A-G.
Introduction
This is going to be an interesting assignment. Basically, we have to recreate the diamond in the picture above using A-G alphabets using this 8086 emulator. Now, this may sound difficult… because it is-but it’s honestly not that bad. Let me explain.
Explanation
In a very oversimplified explanation, I had to first copy the alphabet from A-G into the memory while also separating them with a space. I first placed A (41 in hex value) in memory, then add 1 to it and copy it back to the memory on a different index, plus also copying a space everytime. This keeps going on until it has reached the maximum length. Here is the memory in question.
(41-47 is hex for A-G, 20 is hex for space.)
After that, I’ll gradually increase the string length for printing. Let’s say I want to print A to B, then the string length has to be 3, since in A-B (don’t forget the space) takes 3 bytes in the memory (41, 20, 42 respectively). Once we’ve reached G, I’ll then decrease the string length slowly all the way back to A. You might also be wondering the location to print is, hence the diamond shape. That’s what the DL register is for.
I won’t explain every single thing since it’ll take way too long, so I’ll just briefly go over the DL register. When we want to print something, we can decide on what location we want to print it. We can use the DL register to decide that. If we leave it empty, it’ll print things all the way to the left (beginning). Think of it as printing on the (0,0) coordinate in the cartesian coordinate graph, like the image below. It’ll keep printing on the same X coordinate.
If we add 5 to the DL register, it’ll then print it on the x = 5 coordinate and so on. So we could just set the DL register as 5 from the beginning and keep decreasing it until we reached A-G, where we increase the DL register again until we reached the last A, or the end.
The Code and Execution
Note that it says python, but I couldn’t find a 8086 assembly way in Jekyll to post the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
SET 0
start:
mov ax, 0
mov ds, ax
mov AH, 0x13
mov dh, 0x41
mov bh, 0x20
mov cl, 7
mov ch, 0
_loop_copy:
add ch, 1
mov byte ds[di], dh
add di, 1
mov byte ds[di], bh
add dh, 1
add di, 1
cmp ch, cl
jne _loop_copy
mov cx, 1
mov dl, 6
mov bl, 15
_loop1:
int 0x10
add cx, 2
dec dl
cmp cl, bl
jne _loop1
dec cx
inc dl
_loop2:
sub cx, 2
inc dl
int 0x10
cmp cx, 0
jne _loop2
Conclusion
This wasn’t that bad honestly. I already had a previous code that did something similar to this with the whole copying and printing thing. All we had to do was further manipulate the DL register and change the character printed to be alphabets and voila! It’s done. Here are some good guiding tools you can use if you really want to learn 8086 assembly.