views:

1193

answers:

9

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
+2  A: 

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

Chris Ballance
A: 
+3  A: 

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.

abelenky
+1 for steering him the right direction and actually being helpful.
Esteban Araya
+5  A: 

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.

paxdiablo
So I wrote something similar except I didn't know how to print to STDOUT. Care to explain a little more about mov ah,02h?
Esteban Araya
Esteban, the DOS API is (put something in the ah register to say what functionality you want) and then invoke int 21 (which is the DOS entrypoint). ah=2 is the function to write a character to stdout. For details, see Google for the DOS API e.g. http://en.wikipedia.org/wiki/MS-DOS_API
ChrisW
I don't see cx getting decremented, so I don't understand why the loop exits. Please explain?
abelenky
The loop command decrements cx and jumps to the label if it's non-zero; it's effectively "dec cx; jnz L1;" - see http://web.cecs.pdx.edu/~herb/cs200w03/loops.htm - it's a ripoff of the old z80 instruction DJNZ (decrement and jump if not zero) - there's a blast from the past for you.
paxdiablo
+5  A: 

At a minimum, show some effort. Show code that isn't working, instead of fishing for the full answer...

alphadogg
+4  A: 

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.

Jane Sales
A: 
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.

Esteban Araya
+1 But FWIW, the original question did suggest to start at 0.
lc
my problem is... how can i do the increment process from 0 to 2 is there some any code about it?
And I'll quickly add another hint that you're going to have to decide when to stop "JMP ADDLOOP"ing
lc
This answers that problem btw...(you'll have to make it start at 0 tho).
lc
so it'll bemov ax, 0mov bx, 2addloop:add ax, bxjmp addloop ?is it? T_T
Yes. So then you have to figure out how to exit the loop, and how to output the number.
lc
@Everyone: Yeah, I recognize all of those. I just had to go put the kids in bed.
Esteban Araya
A: 

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

Chuck
Until you get to infinity? :-)
paxdiablo
The ellipsis seemed to imply that was the correct behavior.
Chuck
+2  A: 

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.

ChrisW
That would give 0 2 4 6 8 : < > and so on.
paxdiablo
Thanks, I added to my answer; I didn't read the question fully.
ChrisW
Yes I've just corrected Wikipedia and my answer.
ChrisW