Will someone please explain the use of the stack for me?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Locked
ghostlyfoot
Posts: 5
Joined: Mon Apr 09, 2012 8:40 pm
Location: USA, Mid-south (ie. Mississippi, Tennesee, Arkansas)

Will someone please explain the use of the stack for me?

Post by ghostlyfoot »

I am trying to make use of the stack in a calculator function I am writing for my OS. I want it to store the first number someone types in to the stack, and pop it into the si register to be added with the number with the number in the di register (which was typed in after the push), and then print the si register to the screen. I have created a .bss section with some bytes reserved for the stack in it, but I can not fathom how to use the push and pop instructions with my newly created stack space. Here are some short snippets of my code:

Code: Select all

SECTION	.text
	start:
	mov ax, 0x07C0
	mov ds, ax
	mov es, ax

	mov sp, stack_end

Code: Select all

.calc:
			.calcloop:
				mov si, math_prompt
				call print_string
			
				mov di, buffer
				call get_string

				mov si, buffer
				mov di, math_plus
				call strcmp
				jc .plus

				jmp mainloop

			.plus: 
				mov si, math_prompt_fnum
				call print_string
				
				mov di, buffer
				call get_string

				mov si, buffer
				push stack_end

				mov si, math_prompt_num2
				call print_string

				mov di, buffer
				call get_string

				pop stack_begin

				mov si, di
				mov di, buffer

				add si, di

				call print_string

				jmp .done

			.done: 
				ret

Code: Select all

SECTION .bss
	stack_begin:
		resb 4096
	stack_end:
I also need to tell you that this is kernel code, I am using syslinux as my bootloader, so I do not need to worry about passing 512 bytes. Any help would be appreciated. :)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Will someone please explain the use of the stack for me?

Post by Combuster »

You are mixing up the call stack with the stack datastructure. While both have the same basic operations, there is by design no 1:1 correspondence between calls to parts of the code and parts of the user-supplied equation.

Push and pop are designed for the call stack, and for that stack only. You can't use them for additional stacks.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Will someone please explain the use of the stack for me?

Post by Gigasoft »

This isn't a forum for learning how to program, but the answer to your question can be found here:

http://www.intel.com/content/www/us/en/ ... nuals.html
ghostlyfoot
Posts: 5
Joined: Mon Apr 09, 2012 8:40 pm
Location: USA, Mid-south (ie. Mississippi, Tennesee, Arkansas)

Re: Will someone please explain the use of the stack for me?

Post by ghostlyfoot »

I only ask questions here because there are no other places to ask them. It seems that there is an extreme lack of information in the world of assembly programming, other than the official documentation of the processor, which I cannot comprehend. I guess I'll try to stop, but I don't know where I'll get this information elsewhere. :?
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Will someone please explain the use of the stack for me?

Post by DavidCooper »

ghostlyfoot wrote:I only ask questions here because there are no other places to ask them. It seems that there is an extreme lack of information in the world of assembly programming, other than the official documentation of the processor, which I cannot comprehend. I guess I'll try to stop, but I don't know where I'll get this information elsewhere. :?
Try somewhere like http://forum.nasm.us/
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Will someone please explain the use of the stack for me?

Post by JamesM »

ghostlyfoot wrote:I only ask questions here because there are no other places to ask them. It seems that there is an extreme lack of information in the world of assembly programming, other than the official documentation of the processor, which I cannot comprehend. I guess I'll try to stop, but I don't know where I'll get this information elsewhere. :?
The call stack and stack frames are concepts which exist in all imperative languages. You obviously need to program more before you're fully welcome on this forum.
Locked