Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extremely infuriating piece of code

Status
Not open for further replies.

redwing26

Programmer
Feb 16, 2006
21
GB
Hi I am using asm and I was able to get stuff of my stack a very round about way by first putting the value of the stack into a register e.g cx then doing mov al,cl then calling int 10 to display the hex value ascii code letter.

Anyway I wanted to make it so I did mov al,[bp+4] int 10h but it wont work, Ive been at other places and they said try doing mov ax,cx then int 10 but that doesnt work either here is the whole code below its the 1 value push 45h that should be the only thing on the stack and maybe a pointer to the stack function. The code is much simpler than words see below and check this pastebin if you want to see earlier versions

; --------------------------------------------
; Here is the operating system entry point
; --------------------------------------------
begin:

mov ax, cs ; Get the current segment
mov ds, ax ; The data is in this segment
cli ; disable interrupts while changing stack
mov ss, ax ; We'll use this segment for the stack too
mov sp, 0xfffe ; Start the stack at the top of the segment
sti ; Reenable interrupts
call zap
mov si, msg


; load address of our message

call sortcursor
call putstr ; print the message
call getcurspos
push bp ; save bp
mov bp,sp ; put sp into bp
push 45h
call showstack


hang:
jmp hang ; just loop forever.

; --------------------------------------------
; data for our program


msg db 'Zeon', 0
stx db 'ReadyFI', 0

; ---------------------------------------------
; Print a null-terminated string on the screen
; ---------------------------------------------

zap:
mov ah,06
mov al,00
mov bh,07
mov cx,00
mov dx,184fh
int 10h
retn

putstr:
lodsb ; AL = [DS:SI]
or al, al ; Set zero flag if al=0
jz putstrd ; jump to putstrd if zero flag is set
mov ah, 0x0e ; video function 0Eh (print char)
mov bx, 0x0007 ; color
int 0x10
jmp putstr
putstrd:
retn

sortcursor:
mov ah,02
mov dl,30
mov bh,00
mov dx,00
int 10h
retn

getcurspos:
mov ah,03h
mov bh,00h
int 10h
retn
;Return
;CH = Start scan line
;CL = End scan line
;DH = Row
;DL = Column

showstack:

mov al, [bp+4] ;
mov ah, 0x0e ; video function 0Eh (print char)
mov bx, 0x0007 ; color
int 0x10
;jmp showstack
retn

putstkd:
retn

Thanks in advance if anyone helps Ive just wasted a whole night on this silly problem. Its enough to , drive a man either insane or to the drink lol
 
i really don't understand what you want to do...
but if you just want to display the content of
the stack (say 56)..try tweaking these codes:

push 56h
mov bp,sp

mov al,[bp]
mov bl,al
mov bh,al

shr bh,4
add bh,30h

and bl,0fh
add bl,30h

disp:
mov ah,02
mov dl,bh
int 21h
mov dl,bl
int 21h

i used int 21h function call 02 for displaying a
character. also, the code will only work if the
stack will not contain letters (A-F).
 
Hi that code assembles but doesnt work unfortunately , Ive cut my code back is this problem is driving me crazy , Ive took every call out the program and all the code i have now is

begin:
mov ax, cs
mov ds, ax
cli
mov ss, ax
mov sp, 0xfffe
sti
mov bp,sp
push 56h



Im struggling to get this stack working and I did have int 10 working with one string of the stack yesterday so I really need this working with int 10 if its going to further my understanding. Im already bogged down in like 3 books lol. Any way all I want is to push 56h on the stack now when it gets displayed via int 10h the hex should be intepretted as its ascii value when it comes out like I had it the other day e.g 56h when displayed via int 10 should be V

David

To me I should be able to do

mov cx,[bp] ;supposing i do bp+2 or +4 or +8 or 0 it doesnt work
mov al, cl ;
mov ah, 0x0E ; video function 0Eh (print char)
mov bx, 0x0007 ; color
int 0x10

but it isnt working?


 
You are making a near call to showstack; only the ip is pushed onto the stack prior to the call. Offset to your value is +2, not +4.

The simplest solution is the best!
 
Dear all,

The stack is growing down, not up.

So if you move sp to bp then push the number the only way to get your data is to subtract.

mov bp,sp
push 54h
.....
mov ax,[bp-2] or mov cx,[bp-2]

afther doing the near call

mov ax,[bp-2] or mov cx,[bp-2]

since there are things pushed on the stack but you have saved the stack pointer allready nothing change.

if you have used (and displayd) the character remind to take it of the stack otherwise the stack will get very full afther a while.

Tessa
 
you're right in in that the stack grows down, not up. sp is dec after push; bp is not; the offset to the desired value is -2 relative to bp; so:

mov bp, sp; assume for the moment bp = sp =fffe;
push bp; bp = fffe, sp = fffc
push 45H; bp still = fffe, 0045 @ fffc; sp = fffa

It has been quite some time since I have done anything seriuosly with assembler, and I apologize for the error. Another note, just for GP: The standard practice I have always seen is to set up your stack frame (mov bp,sp; push bp;) after you make the call, not before. If you had done that, then 0045h would be located at [bp+4] as your code is written. It also helps to keep your stack clean, because as long as you keep your pushes and pops even within a subroutine, you don't have to worry about stack corruption.

The simplest solution is the best!
 
As I told you, It has been a long while. After I went back to bed after writing my previous post, it occurred to me that I had made another error; the proper code to set up a stack frame upon entering a subroutine is:

push bp
mov bp,sp ; stack frame is now set
...
pop bp ; leaving the procedure
ret

sorry for all the confusion.


The simplest solution is the best!
 
no probs folks thanks alot for the help I was so frustrated spent 2 days but I hadnt initialised bp properly also in my latest version . I have got it fixed now thanks for all the replies
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top