assembly language help

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
Ive been trying all today to learn how to use subroutines. Firstly im using the NASMIDE compiler. From what Ive been reading, a subroutine is like a function, method etc which can be called several times.

Code:
BITS 16                 ;Set code generation to 16 bit mode
ORG 0x0100              ;Set code start address to 0100h


SECTION .text           		;Main code segment

MAIN: 

		MOV DL, 'A'  		;Moving "A" in DL register
		MOV AH, 02H
		INT 21H			;Tells DOS to execute	
		MOV DH, DL 		;moves DL(A) into DH register
		
                mov  AX, 04C00H     	;This function exits the program
                int  21H

SECTION .data           		;data segment for initialised data & constants



SECTION .bss            		;data segment for uninitialised data

Ok, so this program prints the character A. Now I have to do the same using sub routines. In C, i can imagine making a function

Code:
main()

{
calla();
}

calla()
{
printf("A");
}
Now if I do this with assembly

Code:
BITS 16                 ;Set code generation to 16 bit mode
ORG 0x0100              ;Set code start address to 0100h


SECTION .text           ;Main code segment

MAIN: 

call LABEL

call EXITLABEL

LABEL:

	MOV DL, 'A'  		;Moving "A" in DL register
	MOV AH, 02H
	INT 21H			;Tells DOS to execute	
	MOV DH, DL 		;moves DL(A) into DH register

EXITLABEL:
	
	
        mov  AX, 04C00H     ; This function exits the program
        int  21H


SECTION .data           ; data segment for initialised data & constants


SECTION .bss            ; data segment for uninitialised data

Yes It prints the character A, but I dont think this is a subroutine, anyone who knows assembly can help?:(
 

Cadelin

Resident Freddy
Joined
Feb 18, 2004
Messages
2,514
Why do you not think it is a subroutine?

A subroutine is just a function. It is a bit of code that is effectively self contained and can be executed several times within a program or by other subroutines.

I am not familiar with the exact syntax but the LABEL function does seem to be a subroutine. To demonstrate this you can modify your code to use the subroutine more than once:

MAIN:

call LABEL

call LABEL

call EXITLABEL

I don't really think the EXITLABEL would count as a subroutine as you can only call it at the end to exit the program.
 

phlash

Fledgling Freddie
Joined
Dec 24, 2003
Messages
195
You're just missing the RET instruction at the end of the subroutine, if you have a C compiler handy, try getting it to produce assembly files and having a look at those, with GCC I can do this:

test.c:
---------
int func(int arg) {
return arg * 2;
}

int main() {
func(5);
}
---------

% gcc -S test.c
% vi test.s

HTH, Phil.
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
Ive been led to believe I need to use push and pop onto the stack.

also Ive been led to believe I need to use :

Putch to outputs a char to the screen, the character to be output (the parameter) to go into DL
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
Code:
BITS 16                 ;Set code generation to 16 bit mode
ORG 0x0100              ;Set code start address to 0100h


SECTION .text           ;Main code segment

MAIN: 

call putch
	
        mov  AX, 04C00H     ; This function exits the program
        int  21H

putch:

	MOV DL, 'A'  		;Moving "A" in DL register
	MOV AH, 02H
	INT 21H			;Tells DOS to execute	
	MOV DH, DL 		;moves DL(A) into DH register
	ret


SECTION .data           ; data segment for initialised data & constants


SECTION .bss            ; data segment for uninitialised data

im guessing this is it then, think my tutor just wanted to confuse us
 

rynnor

Rockhound
Moderator
Joined
Dec 26, 2003
Messages
9,353
Shame your not doing IBM Mainframe Assembler as I used to program in it for years - although maybe not a shame from your perspective since the PC version looks a lot easier!!

Maybe I should look into PC based assembler - looks like fun :)
 

Jupitus

Old and short, no wonder I'm grumpy!
Staff member
Moderator
FH Subscriber
Joined
Dec 14, 2003
Messages
3,362
Ahhh good times... PDP 11 .....

I'm old :(
 

Lethul

FH is my second home
Joined
Jan 25, 2004
Messages
8,433
Ive been led to believe I need to use push and pop onto the stack.

also Ive been led to believe I need to use :

Putch to outputs a char to the screen, the character to be output (the parameter) to go into DL

You only need to push and pop if you are gonna alter the registers which your program uses outside the subroutine.
 

Users who are viewing this thread

Top Bottom