Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 643 644 [645] 646 647 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 909555 times)

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9660 on: June 01, 2016, 09:26:06 am »

Hey I need to write an assembly language program that gets two strings from user, then determines which one is longer and how much and prints the results on the screen.

Please look at the code below:

Code: [Select]
data segment
prpt1 db 'Enter the first string: $'
prpt2 db 0Dh, 0Ah,'Enter the second string: $'
msg1 db 0Dh, 0Ah, 'The first string is longer by $'
msg2 db 0Dh, 0Ah, 'The second string is longer by $'
msg3 db 0Dh, 0Ah, 'The strings are of equal length$'
in_str1 db 255, ?, 255 dup('$')
in_str2 db 255, ?, 255 dup('$')
data ends

code segment
assume cs: code, ds: data
start: mov ax, data
    mov ds, ax
; displaying the prompt to user that he should enter the first string
lea dx, prpt1
mov ah, 09h
int 21h
; getting the first string from user
    lea dx, in_str1
    mov ah, 0Ah
    int 21h
; displaying the prompt to user that he should enter the second string
lea dx, prpt2
mov ah, 09h
int 21h
; getting the second string from user   
    lea dx, in_str2
    mov ah, 0Ah
    int 21h
    ; storing the first string length in al, the second string length in ah
    mov al, in_str1+1
    mov ah, in_str2+1
; calculating the difference
    sub al, ah
    ; if we get zero, it means the lengths of the strings are equal, so we inform the user about it and quit the program
    jnz neq
    mov ah, 09h
        lea dx, msg3
        int 21h
        jmp quit   
    ; if we haven't gotten zero that time, we check if the sign flag has been modified then
neq:    js le
; if the sign flag hasn't been changed to 1, then it means that al is positive
xor ah, ah ; flushing ah so that we won't be having problems interpreting what is stored in ax
push ax ; we want to save the difference between the two lengths (which is in al now), so we push ax into the stack
        ; printing the message saying that string in_str1 is longer than string in_str2
mov ah, 09h
lea dx, msg1
int 21h
jmp print
; if the sign flag has not been changed to 1, then it means that al is negative, we need to change its sign
le:    xor ah, ah
neg al
push ax
; printing the message saying that string in_str2 is longer than string in_str1
mov ah, 09h
lea dx, msg2
int 21h
; printing what is in al (the difference between the two lengths), digit by digit
print: xor cx, cx ; we will be using cx to count how many digits are to be printed
mov bx, 10 ; we will be repeatedly dividing ax by 10 and printing the remainder onto the screen
pop ax ; retrieving the difference that we put in the stack previously

loop1: xor dx, dx ; we flush dx so as not to get overflow when dividing
div bx
push dx ; saving the remainder, a digit (reading from right to left) of the number
inc cx
test ax, ax ; we check if the result of dividing is zero. If it is so, then it means we have gotten all the digits of the number
jnz loop1

mov ah, 02h

loop2: pop dx ; retrieving a digit of the difference
add dl, '0' ; getting the code of the coresponding ASCII character in dl
int 21h ; printing the said character
dec cx
jnz loop2

quit: mov ax, 4C00h
int 21h

code ends
end start

and tell me something.

(1) Why do we sometimes get overflow when dividing if the dx register has not been not flushed before the division?
(2) Why do we have to XOR dx, dx before every time we perform division? I noticed that if we don't do that, we may get wrong result it ax and dx. Say, we devide 52h (82) by 0Ah (10), getting 8 in ax and 2 in dx. If we then divide 8 by 10, we get some unexpected 3334h in ax, 0 in dx. How would you explain this?
(3) Can you think of a more reasonable way of handling certain things in the program? Maybe some pieces of it could be simplified or improved in some way.

PS I run it via emu8086
« Last Edit: June 01, 2016, 09:32:57 am by RoguelikeRazuka »
Logged

Shadowlord

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9661 on: June 01, 2016, 10:05:28 am »

(1) Why do we sometimes get overflow when dividing if the dx register has not been not flushed before the division?
(2) Why do we have to XOR dx, dx before every time we perform division? I noticed that if we don't do that, we may get wrong result it ax and dx. Say, we devide 52h (82) by 0Ah (10), getting 8 in ax and 2 in dx. If we then divide 8 by 10, we get some unexpected 3334h in ax, 0 in dx. How would you explain this?

This is why:


(The manual that's in is available at http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html )

Edit:
As for question #3, you could change your le: to:
Code: [Select]
le:
neg al
jmp neq
« Last Edit: June 01, 2016, 10:38:23 am by Shadowlord »
Logged
<Dakkan> There are human laws, and then there are laws of physics. I don't bike in the city because of the second.
Dwarf Fortress Map Archive

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9662 on: June 01, 2016, 12:35:42 pm »

Does anybody have an idea how to write test cases for pathfinding across graphs/constructing spanning trees (of connected subcomponents) of graphs? I've got the functions `depth-first-search`, `breadth-first-search`, `dijkstra-search`, `normal-spanning-tree`, `breadth-spanning-tree` and `dijkstra-spanning-tree`.

I'd like to have some meaningful tests that I can conduct with randomly generated graphs for better coverage. If there is a way to generate certain graphs with relevant properties, I'd need to know how to do that, too.


For spanning trees I can check that
1) the resulting graphs must not contain cycles
2) |V|=|E|+1 must hold for the resulting graph
« Last Edit: June 01, 2016, 12:42:19 pm by Antsan »
Logged
Taste my Paci-Fist

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9663 on: June 01, 2016, 12:59:52 pm »

...

Thank you!  :) I also wanted to ask why isn't it required to put the address of the code segment in the cs register? Like

mov ax, code
mov cs, ax
Logged

TheDarkStar

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9664 on: June 01, 2016, 06:50:22 pm »

Because of this thread, I decided to take a look at SFML. It seems easier than other libraries, but I can't get anything to compile (I get "undefined reference errors"). Are there any things that the tutorial doesn't mention for configuring an SFML project in Code::Blocks? (I have the libraries linked in the right order, I am using the appropriate Windows version, and I'm only using the release mode for building - basically, I'm following the tutorial exactly as far as I can tell)

Edit: After trying different things, it appears that Code::Blocks can't find the .dlls even though they are in the same folder as the executable. Any ideas about why that could be happening?
« Last Edit: June 01, 2016, 08:03:20 pm by TheDarkStar »
Logged
Don't die; it's bad for your health!

it happened it happened it happen im so hyped to actually get attacked now

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9665 on: June 01, 2016, 11:30:18 pm »

Nice to hear that, TheDarkStar! I initially switched to SFML because it gives you much better low-level access, but its also been easier to use too. Getting it working, admittedly, is a bit of a chore though.

SFML Checklist:
1. Under Settings->Compiler->Toolchain executables, make sure you're pointing to the correct MinGW directory. The directory should have a subfolder named bin that contains gcc.exe and such.
2. Under Project->Build Options->Linker settings, make sure you have added sfml-audio, sfml-graphics, sfml-system and sfml-window in that order from top to bottom. Do this for both Release and Debug. For debug, -d needs to be appended to everything.
3. Under Project->Build Options->Search Directories->Compiler, make sure you have added the sfml\include directory. In the Linker tab, add sfml\lib. Again, do this for both Release and Debug.
4. Paste in the code here and check if it will compile. So far, I've discovered that you need to #include <sfml/Graphics.hpp> if you want to access the window and #include <sfml/Window.hpp> if you want keyboard input.

Once you've done all that, you just need to paste sfml-audio-2.dll, sfml-graphics-2.dll, sfml-system-2.dll and sfml-window-2.dll next to your executable. You can change the build location of that under Project->Properties->Build Targets.
« Last Edit: June 02, 2016, 10:25:18 am by DragonDePlatino »
Logged

TheDarkStar

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9666 on: June 02, 2016, 09:49:14 am »

And I solved the problem. I checked the things on the list, but it turns out that my error occurred because I was using DLLs that did not match the version of SFML I was using.
Logged
Don't die; it's bad for your health!

it happened it happened it happen im so hyped to actually get attacked now

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9667 on: June 03, 2016, 11:54:29 am »

So I'm trying to use pdcurses. I downloaded pdc34dllw.zip for windows, extracted it to the folder of the program I'm working on, and #included "curses.h" in the program. Basic program, all it does is call the curses function initscr() and then return 0.

Getting a compiler error, with the location at C:\Users\Username\AppData\Local\Temp\ccLwKeo5.o with the message "program.cpp:(.text+0xc): undefined reference to initscr".

Not sure what I'm doing wrong here. I'm using Orwell fork of Dev C++ with TDM-GCC 4.9.2 32-bit as the compiler.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Shadowlord

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9668 on: June 03, 2016, 12:19:35 pm »

Thanks for reminding me of some of the reasons why I don't use C++ anymore.
Logged
<Dakkan> There are human laws, and then there are laws of physics. I don't bike in the city because of the second.
Dwarf Fortress Map Archive

MoonyTheHuman

  • Bay Watcher
  • I think the DEC VAX hates me.
    • View Profile
    • hellomouse
Re: if self.isCoder(): post() #Programming Thread
« Reply #9669 on: June 03, 2016, 01:46:42 pm »

Because of this thread, I decided to take a look at SFML. It seems easier than other libraries, but I can't get anything to compile (I get "undefined reference errors"). Are there any things that the tutorial doesn't mention for configuring an SFML project in Code::Blocks? (I have the libraries linked in the right order, I am using the appropriate Windows version, and I'm only using the release mode for building - basically, I'm following the tutorial exactly as far as I can tell)

Edit: After trying different things, it appears that Code::Blocks can't find the .dlls even though they are in the same folder as the executable. Any ideas about why that could be happening?
Try using it while writing in assembly
its a pain in the a**

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9670 on: June 03, 2016, 04:59:46 pm »

So I'm trying to use pdcurses. I downloaded pdc34dllw.zip for windows, extracted it to the folder of the program I'm working on, and #included "curses.h" in the program. Basic program, all it does is call the curses function initscr() and then return 0.

Getting a compiler error, with the location at C:\Users\Username\AppData\Local\Temp\ccLwKeo5.o with the message "program.cpp:(.text+0xc): undefined reference to initscr".

Not sure what I'm doing wrong here. I'm using Orwell fork of Dev C++ with TDM-GCC 4.9.2 32-bit as the compiler.
Do you have a program.cpp file in your code somewhere? Looks to me like that's a case where you're trying to use a function/other thing (specifically initscr) that hasn't been defined yet (either because of a missing include, missing libraries in your compiler, typo, or what have you). The whole C:\... stuff is almost certainly just the location where your compiler/IDE generates their temporary compilation, and shouldn't have anything to do with the actual error itself if I'm right.
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9671 on: June 03, 2016, 06:16:04 pm »

Do you have a program.cpp file in your code somewhere? Looks to me like that's a case where you're trying to use a function/other thing (specifically initscr) that hasn't been defined yet (either because of a missing include, missing libraries in your compiler, typo, or what have you). The whole C:\... stuff is almost certainly just the location where your compiler/IDE generates their temporary compilation, and shouldn't have anything to do with the actual error itself if I'm right.

As far as I know the include statement is right. initscr() is a function from the curses.h file so if everything worked correctly then the compiler wouldn't have a problem with recognizing initscr() as a function. What do you mean by "missing libraries in my compiler"? I haven't done anything with the compiler. If I was supposed to do something with the compiler to get the pdcurses header files working, then that's probably what I'm doing wrong.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9672 on: June 03, 2016, 06:49:33 pm »

Undefined reference error is a linker error. You need the .lib or equivalent for you compiler to be linked at this stage.

The header part is the "declaration", whereas the "definition" is the actual body of code.

By missing libraries, it means you haven't linked the pdcurses.lib to your project so that it can look up the actual code.

Spehss _

  • Bay Watcher
  • full of stars
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9673 on: June 03, 2016, 08:04:19 pm »

Undefined reference error is a linker error. You need the .lib or equivalent for you compiler to be linked at this stage.

The header part is the "declaration", whereas the "definition" is the actual body of code.

By missing libraries, it means you haven't linked the pdcurses.lib to your project so that it can look up the actual code.
I have the file pdcurses.lib at least. Not familiar with linking a .lib to a project. Don't think I even have a project set up for this program, actually.

...Googled "dev c++ project library linking". Set up a project and followed process described in a search result. I have pdcurses.lib specified in project options->parameter->linker. Attempted compiling. Same error.

Switched over to codeblocks. Made a new project, moved pdcurses files to project folder, included header with correct pathing. Figured out how to link libraries to projects in that IDE. Linked pdcurses.lib. It works. Guess I'll be going back to using codeblocks for programming c++.
Logged
Steam ID: Spehss Cat
Turns out you can seriously not notice how deep into this shit you went until you get out.

Shadowlord

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9674 on: June 03, 2016, 09:14:14 pm »

maybe dev c++ uses a different .lib format. (Yes, that is a thing - Borland used a different format from Microsoft, for example)
Logged
<Dakkan> There are human laws, and then there are laws of physics. I don't bike in the city because of the second.
Dwarf Fortress Map Archive
Pages: 1 ... 643 644 [645] 646 647 ... 796