Oliver
Oliver

Reputation:

How do I print 0,2,4,6,... in assembly language?

I have an assignment from my comp. system org. subject and unfortunately I'm kind of new when it comes to assembly language. I'm supposed to write a program that displays the numbers 0,2,4,6,8,10 respectively. How would I go about this?

Maybe this'll answer my question: (Reactions please)

.model small
.stack 100H
.data
.code

call proc

mov cx,5

mov dx,0
L1:
mov bx,2
add dx,bx
mov ah,02h
loop L1
int 21
endp

Upvotes: 1

Views: 20005

Answers (9)

s.sundanum
s.sundanum

Reputation: 23

.model small
.stack 100H
.code
.data
var2 DB "10$"
main proc
    mov cx,4
    mov ax,0
    mov dl,al
    add dl,30h
    mov ah,02h
    int 21h
    mov ax,0
    var1:
   add ax,2
   mov dl,al
   add dl,30h
   mov bx,ax
   mov ah,2h
   int 21h
   mov ax,bx
   loop var1
   mov ax,@data
   mov ds,ax
   mov dx,offset var2
   mov ah,09h
   int  21h
   main endp
end main    

I'm new in computer science and when i saw this question i just wanted to try it. I have managed in doing it and here is the code

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882446

Go see your lecturer and/or tutor and ask for advice. That's what they're there for. You haven't given us anywhere near enough info to help you out.

Here's what I think your ABCD program should look like. I suggest you use it as a baseline to try to make a 0 2 4 ... version.

    model  proc
    .stack 100H
    .data
    .call

    main   proc

    mov    cx,10     ; 10 loops only.
    mov    dx,40h    ; start dx at 'A' - 1.
L1:
    inc    dx        ; move to next character.

    mov    ah,02h    ; int 21,02 is print character.
    int    21h

    loop   L1        ; loop until cx is 0

    mov    ax,4c00h  ; int 21,4c is exit with al holding exit code.
    int    21

    endp

When you've at least had a go at converting this, post the code and we'll critique what you've done.

If you're taught something, it never lasts but, if you learn something, it lasts forever (alcohol-addled braincells notwithstanding :-).

Int 21 is the DOS interrupt which allows assembler programs to use various DOS functions. It's conceptually a huge switch statement based on the AH register which is why you'll see things like Int 21 Fn 02, which means execute mov ah,2 followed by int 21.

Int 21 Fn 02 will take the contents of DL and output that to the screen. So the sequence:

mov ah,02h
mov dl,41h
int 21h

will output the 'A' character (0x41).

Similarly, Int 21 Fn 4c will exit the current running process.

Upvotes: 5

Esteban Araya
Esteban Araya

Reputation: 29664

MOV AX, 0
MOV BX, 2

ADDLOOP:
ADD AX, BX
CMP AX, 10
JE DONE
JMP ADDLOOP

DONE:

Ok. That's my best attempt. Lots of details left out. I should also mention that I have no frigging clue how to print a char to the screen.

Like others have mentioned, you didn't specify which assembly language so I chose x86.

Finally, go talk to your instructors, they'll help you much more than we can.

Upvotes: -2

Chris Ballance
Chris Ballance

Reputation: 34367

You do know there is more than one flavor of "Assembly Language."

Upvotes: 3

ChrisW
ChrisW

Reputation: 56123

You can do it exactly like the program which prints A, B, C, D, etc.: except that instead of starting at 'A', start at '0; and instead of increasing by 1 each time (from 'A' to 'B'), increase by 2 (from '0' to '2').

After printing '0', '2', '4', '6', and '8', the next number that you want to print is '10'.

To print '10', you can print '1' followed by '0'. Or, instead of invoking int 21 with ah=2 (which prints one character at a time), you can set ah=9 to print a string (set ds:dx to a block of memory which contains "10$").


Later you suggested the following solution and asked for criticism:

.model small
.stack 100H
.data
.code

main proc

call defineuser1
call defineuser2
mov cx,5

userdefine1 proc
L1:
mov dx,0
mov bx,2
add dx,bx
mov ah,02h
loop L1
int 21h
endp

userdefine2 proc
mov ah, 4ch
int 21h
userdefine2
endp

My criticisms are as follows:

  • defineuser1 doesn't exist (I think you mean userdefine1)

  • setting cx needs to be inside (not before) the procedure

  • invoking int 21 needs to be inside (not outside) the loop

  • you need special handling for "10" as I mentioned above

  • There's a difference between '0' (the ASCII character/digit) and 0 (the number) ... you need to print the character/digit, not the number

  • You need to learn to test your code (write it, step through it with debugger, and debug it), preferably before you post questions about it.

Upvotes: 2

Chuck
Chuck

Reputation: 237110

You would have a counter beginning at zero and repeatedly increment it by two, printing the result.

Upvotes: 0

Jane Sales
Jane Sales

Reputation: 13546

Assembly language is a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU (or architecture). So assembly language for Macs (most recently Intel's X86) is different from that used to on the iPhone - ARM.

Your teacher is also probably expecting you to realise the difference between the binary form of the number you will count with, and the ASCII format you will use to display to the screen.

Upvotes: 4

fortyqueue
fortyqueue

Reputation:

Are you using a macro for the output?

should be something like...

 

        mov eax, 0

myloop: cmp eax, 10 jg done output macro eax add eax, 2 jmp myloop done:

of course that's for 8086 assembly.

Upvotes: -4

abelenky
abelenky

Reputation: 64730

I'm sure your class gave you some education here. Can you code enough assembly to print one or two numbers? Can you code enough to calculate the numbers, even if you can't print them?

Post that much code, and you may find help here.

Otherwise, you're asking others to actually do your homework for you.

Upvotes: 4

Related Questions