Question

i was reading an example in assembly languaje, and i have a little doubt. We were using assembly only on our programs, but the last unit on the semester it's to merge it with turbo c (in-line assembly), and reading the code, there's a part which i don't quite get it: Here's the assembly part:

dosseg
.model small 
.code 
public _myputchar 
_myputchar PROC
    push bp 
    mov bp,sp
    mov dl,[bp+4]
    mov ah,2
    int 21h 
    pop bp 
    ret 
_myputchar ENDP
END

And here's the C part:

#include<stdio.h>
extern void myputchar( char x );
char *str={"Hola Mundo\n"};
void main ( void )
{
    while(*str)
    myputchar(*str++);
    getchar();
}

So, it's pretty straight forward, and the program works, but, what i don't get, it's the assembly code. The problem is, Why the base pointer (bp) it's pointing to +4? (mov dl,[bp+4]), I would think that you only had to mov dl,bp but i don't get why +4. If someone can help we, that would be really apretiated!. (in the include section i put the "" Because the formating tools it's giving me such headech -_-!

Was it helpful?

Solution

The argument (x) is pushed onto the stack before calling the function. After this, the call instruction will push the return address (2 bytes in this case) onto the stack, and the push bp at the beginning of the function will push another 2 bytes onto the stack.

So by now you've pushed 2+2 == 4 more bytes onto the stack after the argument. Since the stack grows downward that means that to get the argument you have to offset the pointer by +4 bytes.

OTHER TIPS

The starting address of the string you want to print is at [bp + 4]. The current stack pointer is [bp]. Remember, the stack grows down.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top