Author Topic: MessageBox is blank.  (Read 9922 times)

Offline Devoum

  • Jr. Member
  • *
  • Posts: 13
MessageBox is blank.
« on: October 27, 2014, 10:05:23 PM »
Hello,

I have been working on this code for two days, trying to get into NASM. I've looked at it through GDB trying to figure out why it does not display the proper message, but I am just not good enough to figure out why...

It is supposed to show the saved EIP as text in the MessageBox. But since the parameter for the text has to be a string, I can't just pass the literal address as a parameter, and have to convert it to ASCII...

Code: [Select]
[BITS 32]

EXTERN _MessageBoxA@16

SECTION .text
GLOBAL _Main

_Main:
call _Cast
call _Convert

push 0x0
push title
push result
push 0x0
call _MessageBoxA@16

ret 0x10

_Cast:
mov esi, esp
mov ecx, 4
mov edx, 8

loop:
xor eax, eax
lodsb

push ax
shr al, 4
and al, 0x0f
mov [buffer+edx*1], al
dec edx

pop ax
and al, 0xf0
mov [buffer+edx*1], al
dec edx

dec ecx
jnz loop

ret

_Convert:
mov esi, buffer
mov ecx, 8
xor ebx, ebx

loop2:
lodsb
and ax, 0x00ff
mov bx, ax

mov dl, [hex+ebx]
mov [result+ecx*1], dl

dec ecx
jnz loop2
ret

SECTION .data

title db "hello",0
hex db "0123456789ABCDEF"

SECTION .bss

buffer resb 8
result resb 8

MessageBox pops up just fine, but it's just displaying a blank area. Please help :/

I'm pretty sure the code is shiet, but it's my first program ever in Assembly.
« Last Edit: October 27, 2014, 10:08:39 PM by Devoum »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: MessageBox is blank.
« Reply #1 on: October 28, 2014, 12:01:41 AM »
Hi Devoum,

I "don't do Windows" but I could test your code and just display it without the MessageBox. First, I thought I'd step through it "thinking like a CPU" (only slower)...

Code: [Select]
_Cast:
mov esi, esp
mov ecx, 4
mov edx, 8

loop:
xor eax, eax
lodsb
; okay, we've got a byte
                ; say it's AB

push ax
shr al, 4
                and al, 0x0f
; now we've got A

                mov [buffer+edx*1], al
dec edx

pop ax
; get our AB back

                and al, 0xf0
                ; now we've got A0
                ; is that what we want?
                ; it isn't going to index into our "hex string"!

That's what I see so far. I think switching to "and"ing with just 0x0f again may help. I'll look some more - maybe try it - but I wanted to give you that hint.

I actually approach this task in a slightly different way. I "rol" the whole number by 4, putting the leftmost nibble (the "first" one to print) to the rightmost position. Then make a copy, mask off the nibble, and convert it to a character - putting it into a buffer in "normal order". When I've "rol"ed the original number the whole 8 times, it's back the way it was...

The way you're doing it should work as well. I'll try to get back to it...

Later,
Frank


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: MessageBox is blank.
« Reply #2 on: October 28, 2014, 04:35:36 AM »
Hi again Devoum,

As expected, you want to "and" with 0x0f, not 0x0f0. There's another problem - you reverse the order of the characters in the first loop, then reverse 'em again in the second loop. I solved it like this:
Code: [Select]
_Cast:
mov esi, esp
mov ecx, 4
mov edx, 8

loop:
xor eax, eax
lodsb

push ax
shr al, 4
and al, 0x0f
mov [buffer+edx*1], al
dec edx

pop ax
and al, 0xf ; <-
mov [buffer+edx*1], al
dec edx

dec ecx
jnz loop

ret

_Convert:
mov esi, buffer
; mov ecx, 8
xor ecx, ecx
xor ebx, ebx

loop2:
lodsb
and ax, 0x00ff
mov bx, ax

mov dl, [hex+ebx]
mov [result+ecx*1], dl

; dec ecx
inc ecx
cmp ecx, 8
jnz loop2
ret

There are other ways to go about it, of course. I didn't try it in an actual MessageBox. It occurs to me that you need a zero-terminated string. Since "result" is the last thing in your code, it is "probably" zero terminated. You might want to add a byte to that buffer and zero-terminate it explicitly so it'll continue to work if you add something after it.

I'd push and pop eax, not just ax, where you do that.  It is "legal" to push/pop 16 bits in 32-bit code, but not often "useful". Doesn't do any harm, but you'll get shorter(!) code if you use eax, besides leaving your stack in a better-aligned state. Really doesn't matter in this case.

Best,
Frank


Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: MessageBox is blank.
« Reply #3 on: October 31, 2014, 01:38:09 PM »
I've played a little bit with your code, Devoum. All my changes are starting at the beginning of a line.

Code: [Select]
[BITS 32]

EXTERN _MessageBoxA@16

SECTION .text
GLOBAL _Main

_Main:
call _Cast
call _Convert

push 0x0
push title
push result
push 0x0
call _MessageBoxA@16

; ret 0x10
ret

_Cast:
mov esi, esp
mov ecx, 4
; mov edx, 8
mov edx, 7

loop:
xor eax, eax
lodsb

push ax
; shr al, 4
and al, 0x0f
mov [buffer+edx*1], al
dec edx

pop ax
; and al, 0xf0
shr al, 4
mov [buffer+edx*1], al
dec edx

dec ecx
jnz loop

ret

_Convert:
mov esi, buffer
mov edi, result
mov ecx, 8
xor ebx, ebx
xor eax, eax

loop2:
lodsb
; and ax, 0x00ff
; mov bx, ax
mov al, [hex+eax]
; mov dl, [hex+ebx]
; mov [result+ecx*1], dl
stosb
dec ecx
jnz loop2
xor eax,eax
stosb
ret

SECTION .data

title db "hello",0
hex db "0123456789ABCDEF"

SECTION .bss

buffer resb 8
; result resb 8
result resb 8+1
« Last Edit: October 31, 2014, 01:55:53 PM by gammac »
Please comment your code! It helps to help you.