Author Topic: Modifying a variable?  (Read 6330 times)

Offline dominick

  • Jr. Member
  • *
  • Posts: 9
Modifying a variable?
« on: August 08, 2011, 05:10:37 AM »
Hi again,

 I know this should be really simple to do - but instead I get some interesting outputs when I try this program... I've modified a variable (a) that is initialized to 0 and it should now hold 1 as a value - but it prints the initial value instead.
I've tried modifying the line 'a equ 0' to 'a dd 0' or 'a resd 0' but that does something even stranger (the output is 364).

Code: [Select]
org 0x100
section .data
a equ 0 ; set a to 0
Newline db 13, 10
Newlinelength equ 2

section .text
And esp, 0x0ffff
;Lea ESI, [a] ; load effective address of a into ESI.
;Mov EAX, 1 ; load 1 into EAX.
;Mov ESI, EAX ; load ESI with EAX.

Mov dword [a], 1 ; load address of a with 1.

push dword a ; push a
Call WriteInteger ; Writes 0 to the screen, instead of 1.
push Newlinelength
push Newline
Call WriteString
mov ax, 0x4c00
int 0x21
WriteInteger:
mov eax, [esp + 2]
mov ecx, 10
xor bx, bx
divide:
xor edx, edx
div ecx
push dx
inc bx
test eax, eax
jnz divide
mov cx, bx
outputDigit:
pop ax,
add al, '0'
mov dl, al
mov ah, 0x06
int 0x21
loop outputDigit
ret 4
WriteString:
mov si, [esp + 2]
mov cx, [esp + 4]
strtoconsole:
lodsb
mov dl, al
mov ah, 0x06
int 0x21
loop strtoconsole
.end:
ret 4

Thanks for your time and patience as I fumble with assembly code,

- Dominick

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Modifying a variable?
« Reply #1 on: August 08, 2011, 05:37:29 AM »
"a" is not a variable in your code, but a constant. See the difference between:

http://www.nasm.us/xdoc/2.09.10/html/nasmdoc3.html#section-3.2.4

and:

http://www.nasm.us/xdoc/2.09.10/html/nasmdoc3.html#section-3.2.1

To use a variable, you'd do something like...

Code: [Select]
org 0x100
section .data

a dd 0 ; set a to 0

Newline db 13, 10
Newlinelength equ 2

section .text
And esp, 0x0ffff

Mov dword [a], 1 ; load "[contents]" of address a with 1.

push dword [a] ; push contents of [a]
Call WriteInteger ; should write 1 to the screen
push Newlinelength
push Newline
Call WriteString
mov ax, 0x4c00
int 0x21
WriteInteger:
mov eax, [esp + 2]
mov ecx, 10
xor bx, bx
divide:
xor edx, edx
div ecx
push dx
inc bx
test eax, eax
jnz divide
mov cx, bx
outputDigit:
pop ax,
add al, '0'
mov dl, al
mov ah, 0x06
int 0x21
loop outputDigit
ret 4
WriteString:
mov si, [esp + 2]
mov cx, [esp + 4]
strtoconsole:
lodsb
mov dl, al
mov ah, 0x06
int 0x21
loop strtoconsole
.end:
ret 4

Untested, but "try that".

Best,
Frank


Offline dominick

  • Jr. Member
  • *
  • Posts: 9
Re: Modifying a variable?
« Reply #2 on: August 08, 2011, 06:34:34 AM »
Thanks *again* Frank  :) ; now I have the same question... but involving strings.
 I've investigated Movs - but nasm doesn't seem to want to use it? Says 'Parser error: Instruction expected'? I guess I have to specify Movsb, Movsw, Movsd? Not sure how Movs works anyway... But I'm guessing that this should be done differently? Perhaps involving cx and that nifty loop. If I try larger strings than 'test' I get a warning about size problems?

Code: [Select]
org 0x100
section .data
b db 0
blength db 0
Newline db 13, 10
Newlinelength equ 2

section .text
And esp, 0x0ffff
Mov byte [blength], 4
Mov dword [b], "test"     ; This works... it does, indeed, print 'test' to the screen... and then a bunch of gibberish.
push word [blength]       ; I guess this does not work; I guess that's why I get a bunch of gibberish after 'test'?
push b
Call WriteString
push Newlinelength
push Newline
Call WriteString
mov ax, 0x4c00
int 0x21
.
.
.

 Super perfundo on the early eve of your day, :)

 Dominick

Offline dominick

  • Jr. Member
  • *
  • Posts: 9
Re: Modifying a variable?
« Reply #3 on: August 08, 2011, 11:44:51 AM »
Nevermind... I forgot that with strings, you have to reserve space. So, instead of 'd db 0' - I used 'd resd 1' and it worked.