5

I've been trying to print a new line while also printing the alphabet using assembly language in nasmide for the past few days and can't get it, what I've tried so far has either printed nothing, printed just A or printed a multitude of symbols, Google hasn't been helpful to me so I decided to post here.

My code so far is

CR equ 0DH
LF equ 0AH

main:
mov AH,02H
mov CX,26
mov DL, 'A'

while1:
cmp DL, 'A'
add DL, 01H
int 21H
mov DL, 0DH
mov DL, 0AH
int 21H
cmp DL, 'Z'
je Next
jmp while1

Next:
mov AH,4CH
int 21h
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user2248734
  • 79
  • 1
  • 1
  • 2

7 Answers7

18

Code for printing new line

MOV dl, 10
MOV ah, 02h
INT 21h
MOV dl, 13
MOV ah, 02h
INT 21h

ascii ---> 10 New Line

ascii ---> 13 Carriage Return

That is code in assembly for new line, code is inspirated with writing machine. Our professor told us the story but I'm not good at english.

Cheers :)

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
CCC
  • 151
  • 1
  • 5
  • 9
    isn't it first 13 and then 10? CR-LF? – xdevs23 Sep 14 '16 at 15:19
  • Yes, CR LF is the standard DOS line-ending order. But if stdout is going to the screen and not redirected to a file, LF moves the cursor down a line and CR returns it to column 0, so both are necessary and their effects are orthogonal and independent of order. That's why this does actually work on the screen, but will make weird files with `program.exe > foo.txt`. (And yes, [`int 21h / ah=02h` is STDOUT](http://spike.scu.edu.au/~barry/interrupts.html#ah02), not the screen specifically) – Peter Cordes Apr 19 '18 at 09:29
  • 1
    The 2nd `mov ah, 02h` is redundant: `int 21h` with `ah=2` preserves AH (and all other registers except AL, where it returns the character written). A few use the full AX for a return value, but AH=02h doesn't. – Peter Cordes Apr 19 '18 at 09:32
4

100% works.

CR equ 0DH
LF equ 0AH

main: 
    mov DL, 'A'

while1:
    mov AH,02H      ;print character
    int 21H 

    mov BL, DL      ;store the value of DL before using DL for print new line

    mov DL, 10      ;printing new line
    mov AH, 02h
    int 21h
    mov DL, 13
    mov AH, 02h
    int 21h

    mov DL, BL      ;return the value to DL

    cmp DL, 'Z'
    je exit 
    add DL, 1       ;store in DL the next character
    jmp while1

exit:
    mov AH,4CH
    int 21h
Igor Osipov
  • 157
  • 1
  • 12
  • It's be easier to use BL as your loop counter, and just have one `mov DL,BL` in the loop as part of printing it. Also, you're *very* close to having [a clean loop structure with a `jcc` at the bottom](https://stackoverflow.com/questions/47783926/why-are-loops-always-compiled-like-this), but just missed. It's also pointless to use `equ` defines for CR and LF if you aren't going to use them. – Peter Cordes Apr 19 '18 at 09:38
  • I think you started with the crappy code in the question and didn't fix most of the ugliness. – Peter Cordes Apr 19 '18 at 09:39
  • This program can be implemented in many different ways, I understand it. I tried not to change the original version of the code, so that the author of the question could easily understand the changes. – Igor Osipov Apr 19 '18 at 10:55
  • If you check the profile of the user that posted this question, they were "Last seen Apr 5 '13 at 11:50", shortly after they posted this question about 5 years ago. They're not going to see your answer. Yes there are multiple ways to implement something, but the best example for a SO answer would be a clean and efficient version that was as simple as possible, removing the overcomplications from the OP's code. – Peter Cordes Apr 19 '18 at 11:04
  • Totally agree with you. You can correct the program to the ideal state and publish it. Perhaps someone in the future will encounter a similar problem and find your answer. The author of the question may already be working at NASA and he does not need our advice :) Best Regards. – Igor Osipov Apr 19 '18 at 11:13
3

Well, first off:

mov DL, 0DH
mov DL, 0AH
int 21H

Isn't going to do you any good. You load 0Dh into DL and then immediately overwrite it with 0Ah without ever having used the first value... You need to make your call (int 21h) on BOTH characters...

Furthermore, you're using DL for newlines overwrites the prior use for the character... You need to save and restore that value as necessary.

Brian Knoblauch
  • 20,639
  • 15
  • 57
  • 92
0

you could just use the

 mov ah, 02h
 mov dl, 13
 int 21h
 mov dl, 10
 int 21h 
 ret

but declare it as a proc at the bottom of your "main endp" you can name that function newline and call it where ever you need a newline

howard howard
  • 49
  • 1
  • 6
0
Mov Ah,02
Mov dl,42
Int 21
Mov dl,0a ---> next line 
Int 21
Mov dl,43
Int 21

Output: 
B
C
yapws87
  • 1,809
  • 7
  • 16
Rynxel
  • 1
  • 3
    Code-only answers are discouraged. Please click on edit and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. Thanks – Nick Dec 08 '18 at 01:34
0
.MODEL SMALL;Code model set to small
.STACK 100H ;Stack memory 100H size
.CODE       ;Code starts from here

START:      ;Mark start of code segment

INPUT:      ;Mark input of code segment
MOV AH, 1   ;AH=1,Single key input
INT 21H     ;Input in AL
MOV BL, AL  ;BL=AL, Input in BL

OUTPUT:     ;Mark output of code segment
MOV AH, 2   ;AH=2,Single key output
 
MOV DL, 0AH ;DL=0AH, ASCII for newline
INT 21H     ;Print DL
MOV DL, 0DH ;DL=0DH, ASCII for carriage return
INT 21H     ;Print DL

MOV DL, BL  ;DL=BL,Display the input 
INT 21H     ;Print DL


Exit:       ;Mark exit of code segment
MOV AH, 4CH ;4CH = DOS exit fuction. Handover the control to OS and exit program
INT 21H     ;Invoke the instruction for interrupt where there function needs to be executed
  • The standard DOS line-ending is CR LF, not LF CR. It has the same effect if printed to the screen, but not when redirected to a file. Also, this question isn't asking for input, it's printing the alphabet. – Peter Cordes Nov 07 '22 at 05:35
-4
mov dl, 0a
int 21h
int 0ah

try this