1

So, I'm trying to just write the logic and make working code, but when I went to run my code to check what I have so far, all the variables were their initialized values. I went as far as to make the first two executed lines just me putting a number in a register then putting it into a variable that I never touch again - variable still had it's declared value.

Here's the code:

section .data


TRUE        equ 1
FALSE       equ 0

EXIT_SUCCESS    equ 0           ; successful operation
NOSUCCESS   equ 1           ; unsuccessful operation

LF      equ 10
NULL        equ 0
ESC     equ 27

SYS_exit    equ 60          ; system call code for terminate
; -----
;  Variables and constants.

STR_LENGTH  equ 15          ; digits in string, including NULL

newLine     db  LF, NULL

; -----
;  Misc. string definitions.


msg         dd 0


; **********************************************************************************

extern  printf

section .text
global  main
main:


mov eax, 10
mov [msg], eax


;  Done

last:
    mov rax, SYS_exit       ; The system call for exit (sys_exit)
    mov rdi, EXIT_SUCCESS
    syscall

Is there something I'm doing wrong? Is there something I should know about coding with global main rather than global _start? Thanks!

EDIT: I'm compiling with these commands:

yasm -Worphan-labels -g dwarf2 -f elf64 as1.asm -l as1.lst

g++ -g -no-pie -o as1 as1.o -lm

EDIT 2:

Also worth noting, the one time a value DID change was when using the XMM0 register:

    movss   xmm0, dword [Num]
    addss   xmm0, dword [One]           ; + 1.0
    movss   dword [Ans], xmm0

Ans would could out to 1. It was declared the same way as msg, only it had a decimal point:

Ans dd 0.0

I assume this is for floating point arithmetic.

EDIT 3:

This also won't work:

;Declared beforehand:
len         dd  60
racism      dd 0
mov eax, dword [len]
mov [racism], eax
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
zapshe
  • 228
  • 1
  • 8
  • 1
    You should not use an exit syscall from main. Anyway, that has nothing to do with your problem. How are you checking the value? – Jester Feb 09 '20 at 02:09
  • I'm checking the value through DDD, using the ```x/dw &msg``` command. I've also checked the values through the execution window with the full code since it outputs it. This was where I first realized none of the variables were having their values change. – zapshe Feb 09 '20 at 02:11
  • You are possibly looking at another symbol with the same name. Verify the address in the disassembled instruction matches the address you are looking at. – Jester Feb 09 '20 at 02:16
  • `msg` is probably a symbol name that glibc also uses, and GDB / DDD can often pick up the glibc address, not your version. (Using `global msg` might help to make it a stronger type of symbol for the debugger's priorities? Or just use a less generic name like `Ans`). It's not plausible that having the program printf the value loaded from `msg` failed, though, unless you did it wrong. – Peter Cordes Feb 09 '20 at 02:17
  • FYI, the code shown works here as expected. – Jester Feb 09 '20 at 02:20
  • I changed the code a tad and the variable name to something that can't possibly be a keyword used somewhere else. Still outputs 0 every time with the ```x/dw &racism``` command. If the code is functioning for you guys, did you edit the code at all? Did you use different lines to build and compile? Did you guys use the same debugger (DDD)? EDIT: Is it possible that there's a needed library installation needed to get this to work? – zapshe Feb 09 '20 at 02:33
  • I used everything as-is, except raw gdb not ddd. – Jester Feb 09 '20 at 02:34
  • Could you try with ddd if it's not too much trouble :(( I've written a lot of code I can't even test because of this. Any help is appreciated. Professor hasn't written back to me at all. – zapshe Feb 09 '20 at 02:37
  • 1
    Won't work how? Show exactly what you're doing with an [mcve] that includes how you're using the debugger. (Copy/paste GDB commands from DDD, or screenshot if you can't copy/paste.) AFAIK, YASM doesn't have any bugs that could explain your code actually doing the wrong thing so it's just a matter of using a debugger properly. You're sure you single-stepped *past* the `mov` store so it already executed? **And use DDD's gdb command to verify the address of your symbols.** (As explained in the linked duplicate; GDB and DDD use the same command language.) – Peter Cordes Feb 09 '20 at 02:41
  • Works fine in ddd too. You did not say you verified the address though. – Jester Feb 09 '20 at 02:42
  • I just verified that the address of the variable is the same one being outputted. It's still 0. It's also verified since if I change the initial initialization to a different number, that's what'll get outputted. Also, I simply run the entire program and check. This is an issue with all the variables I've tried changing my program. When I try single stepping, I'll occasionally get the error that "...init-...c: No such file or directory. This step through happens to show init-misc, though it doesn't stop the program from running and running without stepping doesn't give any errors. – zapshe Feb 09 '20 at 02:55
  • Well, I put a breakpoint right after the mov, and it shows the correct output for the variable. Why does finishing the program reset it to the initialized value? – zapshe Feb 09 '20 at 02:56
  • Interesting, it does that. With the program terminated I wouldn't expect it to show any value. – Jester Feb 09 '20 at 03:00
  • Damn, so I was just being retarded? – zapshe Feb 09 '20 at 03:01
  • 2
    More like confused by unintuitive behavior from the debugger. – Jester Feb 09 '20 at 03:02

0 Answers0