1

Chatgpt says that my code is working, i passed it to the AI for the AI to troubleshoot but it's still not working, why can't i call the function like this?

I should clarify that when i run this insted of hello world some wierd symbols appear, at least it prints something.

PS: after 20 minutes or so i realized that the problem is with the push and pop that i use, i resolved it in a different way but it would be helpful if someone could explane to me why it doesn't work as intended if i use the stack in the way that i did.

[org 0x7c00]

mov bp, 0x7c00 
mov sp, bp 

push message
call printstring

printstring:
    pop bx

    mov ah,0x0e
    .loop:

        cmp [bx], byte 0
        je .Exit

        mov al, [bx] 
        int 0x10
        inc bx

        jmp .loop 
    .Exit:
    ret


message:
    db 'Hello World!',0

encomenda:
    db 'ENCOMENDA REAL!', 0


times 510-($-$$) db 0
dw 0x55aa 

I tried troubleshooting using ChatGPT but the AI didn't helped me at all.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 1
    ChatGPT is not reliable, do not trust it and do not rely on it. – S. C. Jan 05 '23 at 18:31
  • that's why i am asking for help xD – jonasbrocas Jan 05 '23 at 18:31
  • would [this](https://www.viralpatel.net/taj/tutorial/hello_world_bootloader.php) help? – afarrag Jan 05 '23 at 18:33
  • You should also set up the segment registers appropriately. You set SP without setting SS (SS:SP) forms the stack pointer. You don't know where SS points and you are assuming it is 0. Set SS to 0. I'd also set DS to zero as well. I have some general bootloader tips in this answer: https://stackoverflow.com/a/32705076/3857942 – Michael Petch Jan 05 '23 at 19:17
  • I played around with ChatGPT last month creating bootloaders and it did some interestingly bad things. In one case it tried to write to CS, I told it that it can't on all processors, it agreed and fixed it. Then I said the stack segment was wrong and when I told it to correct that it went back to screwing up CS again lol. Of course they do make it clear that the code generated may not be correct, so that warning should be heeded. – Michael Petch Jan 05 '23 at 19:21
  • The start of the code could properly be encoded as `xor ax,ax` `mov ds, ax` `mov ss, ax` `mov sp, 0x7c00` - note: XORing a register with itself has the side effect of making the value 0. – Michael Petch Jan 05 '23 at 19:27
  • I'd also recommend BOCHS and its debugger to step through your bootloader so you can determine by yourself what is happening. – Michael Petch Jan 05 '23 at 19:50
  • @MichaelPetch "I think you want to replace that with `mov bx, [sp+2]` from the stack (the address of the message) and put it in BX." Close but there is no `sp` addressing mode. You have to use either `bp` or pop the return address to some temporary storage to then pop the parameter. In the latter case make sure to restore the return address later. – ecm Jan 05 '23 at 20:09
  • @ecm : Haha, brainphart and I should either take a nap or get coffee. – Michael Petch Jan 05 '23 at 20:18
  • 2
    By popping immediately after entering `printstring` you remove the return address from the stack and put it in BX. Not only does BX not contain the address of the message but the `ret` will fail since there is no return address on the stack anymore. You could replace `pop bx` with `mov bp, sp` `mov bx, [bp+2]`. You also need something like `jmp $` after `call printstring` so that your bootloader doesn't fall into the `printstring` function and start executing that code again. – Michael Petch Jan 05 '23 at 20:36
  • After your `call printstring`, execution will fall through into `printstring` again, and then `ret` will take you to who knows where. You need to trap the instruction pointer with an infinite loop. – puppydrum64 Jan 06 '23 at 12:03

0 Answers0