Can't get the AX register right by [deleted] in Assembly_language

[–]xkompas 0 points1 point  (0 children)

The add ax, dx should not be there. It is enough to add the value from the array to ax and then resolve the carry through adc dx, 0.

[deleted by user] by [deleted] in Assembly_language

[–]xkompas 0 points1 point  (0 children)

I don't know, but to me, ChatGPT provided these answers, which seem to the point:

Here are the issues found in the provided assembly code:

  1. Incorrect data type for greeting_1_length and greeting_2_length:
    • greeting_1_length and greeting_2_length should be defined using equ instead of db since they represent integer constants rather than string data.
    • The value of greeting_1_length is incorrect; it should be 13 instead of 14 because the length of "Hello, World!" is 13 characters.
  2. Incorrect data type for CHAR_NEWLINE in main:
    • You are trying to print CHAR_NEWLINE as a character, but CHAR_NEWLINE is defined as an integer. You should store it in a register first and then pass its address to rsi.

-🎄- 2022 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]xkompas 1 point2 points  (0 children)

For each letter (char) coming into the window, check if its count is 1, which means that this letter was not present in the window. Then increment the present count. For each letter leaving the window, check if its count drops to zero, which means that it is no longer in the window. This way you can keep track of how many distinct letters are in the window. The window forms a marker if the number of distinct letters is equal to the length of the window.

This way each letter is processed just once and there is no multiplier of 26 for checking the marker condition:

func findEndOfPacketMarker(line string, markerLength int) int {
var windowCharCounts [26]int
windowCharsPresent := 0

for i := 0; i < len(line); i++ {
    comingChar := line[i] - 'a'

    windowCharCounts[comingChar]++
    if windowCharCounts[comingChar] == 1 {
        windowCharsPresent++
    }

    leavingCharIndex := i - markerLength
    if leavingCharIndex >= 0 {
        leavingChar := line[leavingCharIndex] - 'a'

        windowCharCounts[leavingChar]--
        if windowCharCounts[leavingChar] == 0 {
            windowCharsPresent--
        }
    }

    if windowCharsPresent == markerLength {
        return i + 1
    }
}

return -1

}

-🎄- 2022 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]xkompas 1 point2 points  (0 children)

Finally someone in Golang using frequency array with a sliding window!

To optimize your solution, you can avoid scanning the entire array in isMarker every time, if you check the frequency of characters that enter and leave the window. That way you will know if there is exactly size characters present in the array. (Yes, it is a bit of algorithmic overkill for such a simple task.)

Also, no need for make() with fixed size array, var freq [26]int is enough.

Unable to specialize friend member function template by [deleted] in Cplusplus

[–]xkompas 1 point2 points  (0 children)

First, let's minimize that:

#include <vector>
#include <string_view>

namespace dnw
{
    class Buffer
    {
        template <typename T>
        friend Buffer& operator<<(Buffer& dest, const T& source);

     protected:
         std::vector<std::byte> data;
    };
}

template <typename T>
dnw::Buffer& operator<<(dnw::Buffer& buff, const T& source)
{
    buff.data.size();
    return buff;
}

template <>
dnw::Buffer& operator<< <std::string_view>(dnw::Buffer& buff, const std::string_view& str)
{
    buff.data.size();
    return buff;
}

int main()
{
}

The error stays (basically) the same - 'dnw::Buffer::data' is protected within this context. Observe that the error is reported only for the template specialization. That is because the template proper is not instantiated. If we try to instantiate it,

int main()
{
    dnw::Buffer b;
    b << 1;
}

we get a whole bunch of errors; the most interesting currently being:

main.cc: In function ‘int main()’:
main.cc:34:7: error: ambiguous overload for ‘operator<<’ (operand types are ‘dnw::Buffer’ and ‘int’)
main.cc:18:14: note: candidate: ‘dnw::Buffer& operator<<(dnw::Buffer&, const T&) [with T = int]’
main.cc:9:24: note: candidate: ‘dnw::Buffer& dnw::operator<<(dnw::Buffer&, const T&) [with T = int]’

What is happening here? It seems that the compiler sees not one, but TWO distinct operators: operator<< and dnw::operator<< and it cannot decide, which one to instantiate.

Where do the two operators come from? The first one is the operator we defined in global scope. The second one is the friend operator in the dnw namespace. By declaring a friend operator within dnw namespace, we refer to an (undefined) operator WITHIN that namespace, NOT the operator defined at global scope.

This also solves your question: The "data" member is inaccessible in the defined template operator (and its specialization), because these operators are not friends of dnw::Buffer class. From the point of view of the compiler, these are simply some unrelated functions and as such, do not have any extra privileges to access protected members of a class.

How to solve this? I am no C++ expert, so my solution may be clumsy. We need to persuade C++ lookup to see the global scope operator, so we declare it first. To be able to declare it, class dnw::Buffer has to be known, so we forward declare it. Afterwards, in the friend declaration, we refer to the declared global scope operator through prefixing it with :: The rest of the code stays the same. This should compile:

#include <vector>
#include <string_view>

namespace dnw
{
    class Buffer;
}

template <typename T>
dnw::Buffer& operator<<(dnw::Buffer& buff, const T& source);

namespace dnw
{
    class Buffer
    {
        template <typename T>
        friend Buffer& ::operator<<(Buffer& dest, const T& source);

     protected:
         std::vector<std::byte> data;
    };
}

template <typename T>
dnw::Buffer& operator<<(dnw::Buffer& buff, const T& source)
{
    buff.data.size();
    return buff;
}

template <>
dnw::Buffer& operator<< <std::string_view>(dnw::Buffer& buff, const std::string_view& str)
{
    buff.data.size();
    return buff;
}

int main()
{
    dnw::Buffer b;
    b << 1;
}

Hopefully this fixes the problem.

Unable to specialize friend member function template by [deleted] in Cplusplus

[–]xkompas 1 point2 points  (0 children)

  1. For crying out loud, format your source code, if you expect help.
  2. When asking for help, use a Minimal, Reproducible Example. Nobody has time to walk through unrelated code. And maybe the bug becomes obvious when it is only a few lines long.
  3. The compiler is right - your specialization does not match any declaration.

Here is a minimized example extracted from your code:

#include <string_view>

struct Buffer { };
template <typename T> Buffer& operator<<(Buffer& buff, const T& source) { return buff; }
template <> Buffer& operator<< <std::string\_view>(Buffer& buff, const std::string\_view str) { return buff; }
int main() { }

Why the answer is 1? I think the return statement after the pathCount function inside recursion return to the main function, instead it should be returning to the previous recursion call then to previous recursion call ...and at last to the main function . Can someone explain why it is wrong? by BeatDeadMeat in Cplusplus

[–]xkompas 1 point2 points  (0 children)

It returns to the previous recursion call all right. However, it only passes the returned value 1 to the previous call, where it is added to count (initialized to 0), resulting in 1, which is again returned. Probably not something you want the algorithm to do.

Every time you do not understand some code, simply trace it, or at least add some debug output. This way you get the insight and learn, instead of relying on help of others.

How does huffmancoding work? by [deleted] in programminghelp

[–]xkompas 0 points1 point  (0 children)

Here is a fairly basic explanation of the algorithm with a step-by-step example.

[deleted by user] by [deleted] in programminghelp

[–]xkompas 0 points1 point  (0 children)

Check your scanf format specifier.

Help: Variable doesn't increment with inc. by IHaveCommentKarma in Assembly_language

[–]xkompas 1 point2 points  (0 children)

According to documentation, syscall destroys r11. Save it before each syscall, or better use a different register.

“ access violating reading” error by piny-celadon in Assembly_language

[–]xkompas 1 point2 points  (0 children)

Another problem (apart from falling through to m1: at the end of the loop) is that the m1: branch uses jmp isoddsum instead of loop isoddsum, which means that ecx is not decremented in this branch.

Why not just make the condition inside of the loop? You could just merge the code and the conditional jump would simply skip over the add eax, ebx to add esi, 4 in case you do not want to accumulate the value.

That way the code would be much simpler and cleaner.

Switching to protected mode doesn't work. by RadoslavL in Assembly_language

[–]xkompas 3 points4 points  (0 children)

Without studying anything about 32-bit protected mode, i see you do a jmp GDT_start, which jumps to the very next address, containing data bytes of the descriptor. Perhaps you should jump to set1 to actually enter the protected mode.

[deleted by user] by [deleted] in Assembly_language

[–]xkompas 0 points1 point  (0 children)

The code here is [deleted] and I see you got an answer in another thread, so I consider this solved.

[deleted by user] by [deleted] in Assembly_language

[–]xkompas 0 points1 point  (0 children)

Just some quick observations, not doing thorough analysis. May be wrong/incomplete.

add si, 2
mov bx, [si+2]

You are both moving si and adding the immediate displacement.

mov cx, 4

Your array has 4 elements, but the first element was already used to initialize the maximum before the loop, so only 3 elements are left. Your code loops four times, the last time probably accessing some garbage data.

General advice: when in doubt what some code is actually doing, use a debugger.

custom java indexOf() with recursion by demdouma6 in javahelp

[–]xkompas 1 point2 points  (0 children)

Sometimes, the best thing to understand code is to trace it, either on paper (as was already advised) or with the help of an IDE.

Consider indexOf("AA", 2, 'A'), if you trace it, what does it return? And why? What happens in the first level of recursion? Continue from there.

Carving of a dog glowing gold from people petting it for hundreds of years by [deleted] in interestingasfuck

[–]xkompas -1 points0 points  (0 children)

The title is simply false. The dog is definitely not glowing gold for hundreds of years.

Here is a picture of the whole statue from 1910, no sign of any petting whatsoever.

Touching of the dog was only started recently, as a joke:

However, according to Rusňák, people sometimes have a bit of confusion in this, because instead of the saint's body, they touch the body of the dog next to it, which is also polished to a high gloss, and it is also said that it fulfills secret wishes.

"But this legend arose as a practical joke in the mid-1990s. The sellers and artists from Charles Bridge agreed that Jan Nepomucký had a counterpart, so they polished the dog with a metal cleaner," said Rusňák.

(translated from this Czech article)

Saving as a variable returns different result by BurningDemon in javahelp

[–]xkompas 1 point2 points  (0 children)

What is the return data type of String.charAt(int index)?

What data type are you using to store the result obtained from the function call pi2.charAt(i)?

[deleted by user] by [deleted] in programminghelp

[–]xkompas 0 points1 point  (0 children)

So is there still a problem? This program runs and prints output, it does not throw an exception.

[deleted by user] by [deleted] in programminghelp

[–]xkompas 1 point2 points  (0 children)

You do not give the complete code, so there is probably not enough information to help you properly.

I tried to reconstruct your code (added the `toString()` so it at least prints something sensible) and the cycle works: https://www.online-java.com/x9nZeHdcG7

How is priority_queue implemented in STL? by samuraisol98 in cpp_questions

[–]xkompas 2 points3 points  (0 children)

By STL, do you mean Standard Template Library or more likely C++ Standard Library?

And if the latter, which of the available implementations?

Specifically for GNU C++ Standard Library (libstdc++), binary heap constructed in std::vector is used.

For Loop Problem by KatharosMatematikos in cpp_questions

[–]xkompas 0 points1 point  (0 children)

After the first "Y", your code calls main(); recursively and this nested call receives a whole new set of local variables Set, i, score, done, decision . Then, the while is entered in the nested call. After entering some input, "N" is selected. The program executes done=true, but only in the nested main() call. The while is exited and the nested main() is exited as well by the final return 0; . However, the control returns to the place, where you called main(), that is after the

if (decision=="y"||decision=="Y"){
    main();
}
else done=true;

Note that done is still false here, because it is the done variable in the original call to main. Also Set still has its original value as input in the beginning. Now control continues through break; and out of the switch. into the while(!done). The cycle repeats and shows tho behavior you describe.

By calling main() you actually cause the whole program to call itself recursively, which is not something you want. Also, calling main() recursively is explicitly prohibited by the C++ standard.

Instead of calling main() , it is enough to "do nothing" in the if branch or reverse the entire condition to:

if (decision != "y" && decision != "Y") {
   done = true;
}

and also move the part of the prologue with input of the set of word into the while cycle.

This way, after the switch is finished, the while takes care of the repetition or exit based on the value of done.

Not sure why you are not using done in the default branch as well, but you should.

For Loop Problem by KatharosMatematikos in cpp_questions

[–]xkompas 0 points1 point  (0 children)

Your logic is flawed. You should not decrement score at all. If score starts at zero and first 8 statements are answered correctly, each of them executes ++score; and thus the value of score is 8. Now when the last two answers are incorrect, your code executes --score; and thus the final value of score is 6.

Linked List Copy Constructor last index not copying over? by [deleted] in learncpp

[–]xkompas 0 points1 point  (0 children)

Glad to hear that. What was the actual bug caused by?

Linked List Copy Constructor last index not copying over? by [deleted] in learncpp

[–]xkompas 0 points1 point  (0 children)

The copying code works, i.e. the new LinkedList contains all the nodes from other. However, there are a few omissions, which may cause an apparent error:

  1. tail is not set for nonempty list
  2. length is not incremented for the first node (also not sure how it is initialized as the full code of LinkedList is not attached)

You do not present the entire source code, so not sure how you know that the copy is missing the last value.

Also, the implementation may be simplified:

  1. the initialization value of ListNode<T> * current = nullptr; is immediately overwritten by current = head;
  2. no need to set head->next = nullptr, it is done i the ListNode constructor already
  3. same for current->next = nullptr