5

I'm learning assembly language and I have a doubt. I'm programming a simple "hello world" with this code:

.model small
.stack
.data
    message db 'Hello world! $'
.code
start:
    mov dx,@data
    mov ds.dx

    lea dx,message
    move ah,09h
    int 21h

mov ax,4c00h
int 21h
end start

I'm assuming that message db 'Hello world! $' works like a String, and now I'm wondering if it is possible to add something like \n to make the output in two lines, like this message db 'Hello\nworld! $'. Is that possible?

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 2
    Depends on your assembler that you didn't specify. You might have to insert the ascii code by hand if your assembler doesn't support `\n` style escape. – Jester Dec 11 '16 at 21:35
  • `db 'Hello'` is "convenience" to define bytes easily, when their values are easy to define in ASCII encoding ... like strings mostly... But it is the same as writing `db 72, 101, 108, 108, 111` => defining five byte values (equal to "Hello" when viewed as ASCII string). While you will rarely *want* to define non-string data through ASCII encoding, you *can*. – Ped7g Dec 11 '16 at 22:15

2 Answers2

9
message db 'Hello world! $'

Many assemblers will not interpret the \n embedded in a string.
Most assemblers will accept the following to insert a newline:

message db 'Hello',13,10,'world!',13,10,'$'

The value 13 is carriage return, and the value 10 is linefeed.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • What if I wanted to store 13, 10 in some sort of variable, so I could, instead of calling 13, 10 call a variable that did the same thing? Eg: message db 'Hello',13,10,'world!', newLineVar'$' –  Jan 15 '22 at 23:21
  • @Triangle4 A preprocessor directive would do the trick. In FASM you would write `newLineVar equ 13,10`. Then the preprocessor will replace all occurences of the symbolic constant *newLineVar* by its value *13,10*. Other assemblers have similar concepts (`%define`, ...). – Sep Roland Jan 23 '22 at 14:39
  • What about a preprocessor in Masm? And you seem to know a lot about assembly, is there a good resource I can use to learn the language? I find it shares a lot of similarity to c and c++ and cannot help but think it would be invaluable to understand it better. –  Jan 24 '22 at 20:38
-4

Worked for me (8086 Assembly):

.MODEL SMALL
.STACK 100H 
.DATA
LOADING DB 'Starting LunaOS...','$'
DONELOADING DB 'Starting LunaOS... done.','$'
.CODE

MOV AX,@DATA
MOV DS,AX

LEA DX,LOADING
MOV AH,9
INT 21H    

LEA DX,DONELOADING
MOV AH,9
INT 21H    

;LEA DX,STRING2
;MOV AH,9
;INT 21H  

;LEA DX,STRING3
;MOV AH,9
;INT 21H 

;LEA DX,STRING4
;MOV AH,9
;INT 21H 

MOV AH,4CH
INT 21H   


END

To add a new line, copy the LEA DX,(STRING NAME) and copy the MOV AH, 9. Then copy INT 21h, paste it to a new line, add a string to ".DATA", change the LEA,DX(STRING NAME) to LEA,DX(NEW STRING NAME)