0

Write an assembly procedure called increment that is callable from C. The procedure should take a pointer to 32-bit integer as a parameter, and should increment the integer to which the parameter points. The C prototype for the function is as follows:

void increment (int *p);

provide only the assembly code from the procedure label to the ret instruction.

So, the question has to be done in Assembly x86-64 (in this case, E registers since it's asking for 32-bit). It's generally a short segment of logical instructions of 3-12 lines of code in _start: that I have to code assuming everything else is already declared and defined. For example:

mov rax,[x]

mov rbx,[y]

cqo

idiv rbx

mov [q],rax

mov [r],rdx

My issue is it's asking for the procedure to take a pointer to a 32-bit integer as a parameter AND incrementing the integer to where the parameter is pointing.

I'm not sure how I would go about in translating this C code into NASM, or a callable function:

void increment  (int *p) {
    *p = *p + 1;
} 
Atlas
  • 11
  • 3
  • Which part do you have a problem with? Can you access the argument? Can you dereference a pointer? Can you do addition? Note that the size of the pointer does not depend on what it points to so it's still 64 bit. – Jester Dec 15 '21 at 12:39
  • 1
    `void increment (int p);` takes a value, not a pointer. But apparently you had `void increment (int* *p)` but didn't use code-formatting to protect the asterisks from being markdown. So that's too many levels of indirection, a pointer to a pointer. If that was actually part of the assignment, it's buggy and doesn't match the text which tells you to take a pointer to an integer. Anyway, use a C compiler to have it make an example for you. [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) – Peter Cordes Dec 15 '21 at 12:46
  • @PeterCordes Oh, I am so sorry about that. I tried to italicize but it seems like the asterisk was put there. I fixed it. Again, sorry for the confusion. – Atlas Dec 15 '21 at 12:59
  • 2
    Try something like `increment: inc dword [rdi]; ret` – fuz Dec 15 '21 at 13:23
  • Hey @fuz sorry for the late response, didn't get the notification. Would that be the first part of the line within the _start: section? Since it's a 32-bit wouldn't you be using an edi register, instead of rdi? – Atlas Dec 15 '21 at 21:46
  • @Atlas This would be the implementation of the `increment` function. In 64 bit mode, pointers are 64 bit long, regardless of what data type they point to. Hence `dword` (for the size of the datum you want to increment) but `[rdi]`. – fuz Dec 15 '21 at 21:47
  • @fuz sorry in advance for the dumb questions, as this is my first time ever coding in Assembly, but I assume there are more than just one line of code after incrementing such as scanf, printf, .L1? Or is that basically it? – Atlas Dec 15 '21 at 21:59
  • @Atlas The function you want to translate does not have a call to `printf` or `scanf`. I do not understand why you have calls to these functions in the first place. – fuz Dec 15 '21 at 22:04
  • @fuz Would I be able to show you through visual representation either through screen share really quick? If not I'll try my best to explain it here; I have a C file in replit that the .asm file has to call to, and I know from textbook scanf is an important detail for C language. If you say it's not important in this aspect, then that line of code is basically all I would need in order to follow the question on hand? – Atlas Dec 15 '21 at 22:17
  • @Atlas The question seems to ask you to call into the ASM code from C, not the other way round. I don't see anything about `scanf` and `printf` in it. Perhaps there are some unstated assumptions in it. But if the question is just to translate that one function, the code I gave is sufficient. – fuz Dec 15 '21 at 22:34
  • @fuz Gotcha. Just to double-check, the bolded question is what you're referring to correct? – Atlas Dec 15 '21 at 22:44
  • @Atlas Yes, correct. – fuz Dec 15 '21 at 22:49
  • @fuz Oh and I just noticed, why the semicolon after [rdi] ? Isn't semicolons used to make comments? – Atlas Dec 15 '21 at 22:52
  • @Atlas I used that to separate the two instructions. You might need to place the instructions on separate lines instead. – fuz Dec 16 '21 at 01:03
  • Figures just wanted to make sure it wasn't a comment on purpose. – Atlas Dec 16 '21 at 02:50
  • @fuz Out of the blue, but...doesn't `increment: inc dword [rdi]; ret` have no way of callable to C function? – Atlas Dec 16 '21 at 17:41
  • Well you have to export the symbol of course, but then it is perfectly callable. – fuz Dec 16 '21 at 20:38
  • @fuz So it would still be callable without? If not, which code would I use to export the symbol? – Atlas Dec 16 '21 at 20:43
  • 1
    It is callable without being exported, just not visible. To make it visible, write `global increment` – fuz Dec 16 '21 at 20:49

0 Answers0