hey, guys.
I got a problem with the string variable.
When I control a string, i used to do it like this.
code segment
assume cs:code, ds:data
start:
mov dx, offset str
mov ah, 09h
int 21h
code ends
data segment
str db "I do like this.$"
data ends
ends start
There is no problem here. It works good.
But When we translate this into c. The variable of str is becoming a globle variable.
What i want to ask is how to use a local variable in Assembly.
I mean how to translate the following c code into the Assembly.
int howto()
{
string str;
str = "Nice to meet you, guys.";
print(str);
}
// assume that we have a print function that can print string
// into console.
Many thanks.
&
Best regards.
Comments
:
: I got a problem with the string variable.
:
: When I control a string, i used to do it like this.
:
: code segment
: assume cs:code, ds:data
: start:
: mov dx, offset str
: mov ah, 09h
: int 21h
: code ends
: data segment
: str db "I do like this.$"
: data ends
: ends start
:
: There is no problem here. It works good.
: But When we translate this into c. The variable of str is becoming
: a globle variable.
: What i want to ask is how to use a local variable in Assembly.
:
: I mean how to translate the following c code into the Assembly.
:
: int howto()
: {
: string str;
: str = "Nice to meet you, guys.";
: print(str);
: }
: // assume that we have a print function that can print string
: // into console.
:
:
: Many thanks.
: &
: Best regards.
:
:
:
[color=Blue]Local variables are stored on the stack, so the idea is to decrease the stack pointer in order to allocate memory from the stack segment. When you no longer need the local variable, increase the stack pointer again.[/color]
[code]
.model small
.stack 64
data segment
str1 db "Nice to meet you, guys.$"
data ends
code segment
assume cs:code, ds:data;, es:data
start:
call howto
; exit
mov ax,4c00h
int 21h
[color=Grey]; int howto()[/color]
howto:
[color=Blue]; Allocate memory for local variable - str[/color]
[color=Grey]; string str;[/color]
sub sp, 48
[color=Blue]; Copy str1 to str[/color]
[color=Grey]; str = "Nice to meet you, guys.";[/color]
mov ax, seg data
mov ds, ax
mov di, offset str1
xor cx, cx
mov si, sp
copystr:
mov al, ds:[di]
mov ss:[si], al
inc di
inc si
cmp al, "$"
jne copystr
[color=Grey]; print(str);[/color]
mov dx, sp
mov ax, ss
mov ds, ax
call print
[color=Blue]; Restore stack pointer (deallocate locals)[/color]
add sp, 48
ret
print:
mov ah, 09h
int 21h
ret
code ends
end start[/code]
[color=Blue]Feel free to ask more questions if you need to!:-)[/color]
Thank you very much for you kind reply.
I understood what are you trying to do. And it is a good solution.
But i still have a question. You set the string variable in the data segment
at the beginning. If you do like this, it means this variable will be
allocated in the memory from the start to the end of this program.
You will never free the memory space of the string variable.
If the memory space will never be freed, then why not save the point of
this variable into the stack. And delete the point from the stack when we
break out from the block.
Another question is, save a string that inputed from keyboard into
a local variable. In this situation you cannot save the string in the
data segment.
Thank you again. & Have a nice day.
: Thank you very much for you kind reply.
:
: I understood what are you trying to do. And it is a good solution.
: But i still have a question. You set the string variable in the data
: segment
: at the beginning. If you do like this, it means this variable will be
: allocated in the memory from the start to the end of this program.
: You will never free the memory space of the string variable.
[color=Blue]Yes. The string in the data section is constant and global (even in the C program), therefore the memory it uses is never deallocated. Just like you don't free any code that has already been run, you don't free constant data when you've already used it. However, if you're sure you won't need the data anymore in the rest of the program, then you may, of course, overwrite it and use it for something else.
[/color]
: If the memory space will never be freed, then why not save the point
: of this variable into the stack. And delete the point from the stack
: when we break out from the block.
[color=Blue]
Absolutely. The code I gave was merely the exact asm equivalent of your C code. Surely it would be more efficient to use the first asm program that you gave, which goes like this in C:[/color]
[code]
int howto() {
print("Nice to meet you, guys.");
}
[/code]
[color=Blue]But as I already explained, you can't deallocate the "Nice to meet you, guys." string, because it's not really a local variable, it's global data. If it was deallocated, then if you were to call howto twice, the string wouldn't exist anymore.[/color]
[code]
[/code]
: Another question is, save a string that inputed from keyboard into a local variable. In this situation you cannot save the string in the data segment.
[code]
[/code]
[color=Blue]Why's that?[/color]
[code]
.model small
.stack 64
data segment
strbuffer db 48 dup(?)
data ends
code segment
assume cs:code, ds:data, es:data
start:
call getstring
[color=Blue]; Print strbuffer[/color]
mov dx, offset strbuffer
mov ax, seg data
mov ds, ax
call print
; exit
mov ax,4c00h
int 21h
getstring:
[color=Blue]; Allocate memory for input buffer[/color]
sub sp, 48
mov ax, ss
mov ds, ax
mov dx, sp
mov di, sp
mov byte ptr ss:[di], 46 [color=Blue]; maximum size of input[/color]
mov ah, 0ah
int 21h [color=Blue]; get user input[/color]
[color=Blue]; Copy string from stack to data segment[/color]
mov ax, seg data
mov es, ax
mov ax, ss
mov ds, ax
cld
xor cx, cx
mov di, offset strbuffer
mov si, sp
mov cl, ss:[si+1] [color=Blue]; nr of bytes read[/color]
add si, 2
rep movsb
mov byte ptr es:[di], "$"[color=Blue] ; Terminate string for printing[/color]
mov dx, offset strbuffer
mov ax, seg data
mov ds, ax
call print
[color=Blue]; Restore stack pointer (deallocate locals)[/color]
add sp, 48
ret
print:
mov ah, 09h
int 21h
ret
code ends
end start
[/code]
Wow. Your explanation makes my head so clear about this problem.
Actually, the code you wrote is the answer that i wanted at the first time.
But now, i got more than that.
Thank you very much.