Using esp register

I was trying to figure out how to use stack with assembly, and in my attempt I came across the following code in one of the questions on SO, namely:

push    ecx

mov eax, 4
mov ebx, 1
mov ecx, result
mov edx, result_len
int 0x80

mov eax, 4
mov ebx, 1
mov ecx, esp
add [ecx], DWORD 48
mov edx, 2
int 0x80

      

In this case ecx holds the number and the author displays it as a number (correct me if I'm wrong!) First by moving the stack pointer to ecx and then converting the number to ascii by adding 48 to memory the address ecx points to. Can it do the same "pop ecx" and then convert to ascii? I don't quite understand why the author is doing this. Any help would be appreciated.

+3


source to share


3 answers


Can he do the same with "pop ecx" and then convert to ascii?



Not. Sys_write system call, a pointer to a string is required for printing. By pushing onto ecx

the stack, you create a pointer (address) to esp

.

+2


source


Explain line of code line by line

push    ecx #ADDS THE VALUE IN THE REGISTER ECX TO THE STACK (TO SAVE IT FOR LATER USE...)

mov eax, 4 #USE stdout AS OUR OUTPUT
mov ebx, 1 #USE stdout AS OUR OUTPUT
mov ecx, result #POINTER TO THE MEMORY ADDRESS OF THE CHARACTERS TO OUTPUT
mov edx, result_len #MAX NUMBER OF CHARACTERS TO SHOW
int 0x80 #EXECUTE THE INTERRUPTION 0X80

mov eax, 4 #USE stdout AS OUR OUTPUT
mov ebx, 1 #USE stdout AS OUR OUTPUT
mov ecx, esp #MOVE THE POINTER FROM THE STACK POINTER OT THE ECX REGISTER THAT LETS US ACCESS EXC IN THE STACK
add [ecx], DWORD 48 #ADDS TO THE VALUE INSIDE OF THE ECX REGISTER THE VALUE OF 48 AS A DWORD TO FIT THE EXTENDED SIZE 
mov edx, 2 #MAX NUMBER OF CHARACTERS TO SHOW
int 0x80

      



What I'm guessing is that it gets the value of something and stores it on the stack, then prints the string to stdout, after which it gets that value previously stored in the stacks, and adds it to all registers, and finally outputs it ...

This might help as well: http://www.tutorialspoint.com/assembly_programming/assembly_system_calls.htm

0


source


If we use EAX, EBX, ECX or EDX as the address register inside the brackets, then DS is the default segment. But if we use ESP or EBP then SS is the default segment.

So, the instruction: "add [ecx], DWORD 48" add the dword value to the address pointed to by DS: ECX.

But we can add a segment override prefix to our statement to override the default segment with a different one. Example: "add SS: [ecx], DWORD 48".

0


source







All Articles