I am new in assembly and I had my first lesson few days ago.
I got an assignment to add my name to a program that prints "Hello,world!" and I successfully did that, but I have a strange space between the "Hello,world!" and my name.
Can someone tell me how I can fix that and why does it happen?
Here is my code:
name "hi-world"
; this example prints out "hello world!"
; by writing directly to video memory.
; in vga memory: first byte is ascii character, byte that follows is character attribute.
; if you change the second byte, you can change the color of
; the character even after it is printed.
; character attribute is 8 bit value,
; high 4 bits set background color and low 4 bits set foreground color.
; hex bin color
;
; 0 0000 black
; 1 0001 blue
; 2 0010 green
; 3 0011 cyan
; 4 0100 red
; 5 0101 magenta
; 6 0110 brown
; 7 0111 light gray
; 8 1000 dark gray
; 9 1001 light blue
; a 1010 light green
; b 1011 light cyan
; c 1100 light red
; d 1101 light magenta
; e 1110 yellow
; f 1111 white
org 100h
; set video mode
mov ax, 3 ; text mode 80x25, 16 colors, 8 pages (ah=0, al=3)
int 10h ; do it!
; cancel blinking and enable all 16 colors:
mov ax, 1003h
mov bx, 0
int 10h
; set segment register:
mov ax, 0b800h
mov ds, ax
; print "hello world"
; first byte is ascii code, second byte is color code.
mov [02h], 'H'
mov [04h], 'e'
mov [06h], 'l'
mov [08h], 'l'
mov [0ah], 'o'
mov [0ch], ','
mov [0eh], 'W'
mov [10h], 'o'
mov [12h], 'r'
mov [14h], 'l'
mov [16h], 'd'
mov [18h], '!'
mov [20h], 't'
mov [22h], 'e'
mov [24h], 's'
mov [26h], 't'
; color all characters:
mov cx, 12 ; number of characters.
mov di, 03h ; start from byte after 'h'
c: mov [di], 11101100b ; light red(1100) on yellow(1110)
add di, 2 ; skip over next ascii code in vga memory.
loop c
; wait for any key press:
mov ah, 0
int 16h
ret
mov [02h], 'H' mov [04h], 'e' mov [06h], 'l' mov [08h], 'l' mov [0ah], 'o' mov [0ch], ',' mov [0eh], 'W' mov [10h], 'o' mov [12h], 'r' mov [14h], 'l' mov [16h], 'd' mov [18h], '!'
The offset addresses that are being used in these instructions are expressed in the hexadecimal number representation and are being incremented by 2.
Looking at
mov [08h], 'l' mov [0ah], 'o'
we see that the next word after 08h is not at 10h but rather at 0Ah. Hexadecimal uses the digits {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}. Therefore incrementing 8 by 2 will produce A.
If you want your additional text to follow immediately behind the exclamation mark, then a similar change has to be taken after:
mov [18h], '!'
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
^ ^ ^ ^ ^ ^ ^
H e l l o , W
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
^ ^ ^ ^ ^
o r l d ! <-- the 3-character gap that you see
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
^ ^ ^ ^
t e s t
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
^ ^ ^ ^ ^ ^ ^
H e l l o , W
10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
^ ^ ^ ^ ^
o r l d ! r o e
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F
e