Search code examples
assemblymemorystackemu8086

Pushing registers to memory stack


When we are pushing a register to the memory stack, what does it enable us to do?Does it just simply help us perform operations which dont fit in the AL,AH registers?

I had to write a program for a computer which has the x8086 processor and I had to find the equivalent time in hours of 35600 seconds so the AL,AH registers were to small to perform the division 35600/3600


Solution

  • There's quite a few things you can do with push:

    • Storing a value so that you can use it later.

    On the 8086, you can only bit shift or rotate by either 1 or the value in the CL register. This can become a problem if you're using CX as a loop counter but you're trying to bit-shift during your loop. So you can fix it with this:

    foo:
    push cx
       mov al,[si]
       mov cl,2
       shl al,cl
       mov [di],al
    pop cx
    loop foo
    
    • Reverse the order of letters in a string.
    .data
    myString db "Hello",0
    .code
    
    mov si,offset myString
    mov ax,0
    mov cx,0
    cld
    
    stringReverse:
    lodsb          ;read the letter (and inc si to the next letter)
    cmp al,0       ;is it zero
    jz nowReverse  ;if so, reverse the string
    push ax        ;push the letter onto the stack
    inc cx         ;increment our counter
    jmp stringReverse
    
    nowReverse:
    mov si,offset myString  ;reload the pointer to the first letter in myString
    
    loop_nowReverse:
    jcxz done               ;once we've written all the letters, stop.
    pop ax                  ;get the letters back in reverse order
    stosb                   ;overwrite the old string
    dec cx                  
    jmp loop_nowReverse
    done: