Posts

Showing posts with the label assembly

C - print args without stdlibs

C - print args without stdlibs I just wrote a C program that prints its command line argument without using the standard library or a main() function. My motivation is simply curiosity and to understand how to play with inline assembly. I am using Ubuntu 17.10 x86_64 with the 4.13.0-39-generic kernel and GCC 7.2.0. main() Below is my code which I have tried to comment as much as I understood. The functions print , print_1 , my_exit , and _start() are required by the system to run the executable. Actually, without _start() the linker will emit a warning and the program will segfault. print print_1 my_exit _start() _start() Functions print and print_1 are different. The first one prints out a string to the console, measuring the length of the string internally. The second function needs the string length passed as an argument. The my_exit() function just exits the program, returning the required value, which in my case is the string length or the number of command line arguments. ...

Not understanding FLAGS status register

Not understanding FLAGS status register I was helping a friend of mine doing some computer architechture theory exercises. We stumbled on this m.c. exercise about the FLAGS register and don't really know how to answer it. It goes on something like this: "Assume that you've performed an ADD operation between two unsigned integer numbers, and that it resulted in the following flags setting: CF = 0; OF = 1; ZF=0; SF=1 . What can we conclude from this setting and why?" The correct choice states that the result of that operation is correct, and when saying why, it states " because the CF equals zero". My question here is why do we only need to consider the carry flag when there are other flags set to one? And also, how is possible for the overflow and the signed flag to be set to one when we are performing an add operation between to unsigned integers? Thanks in advance! 2 Answers 2 ...

What use is END OF MEMBER In IBM Assembly

What use is END OF MEMBER In IBM Assembly I am parsing some IBM Assembly Language which also happens to be a BMS map. The code looks like this: DFHMSD TYPE=FINAL END END OF MEMBER The END OF MEMBER statement is not causing any syntax errors on the mainframe. END OF MEMBER Why is it syntactically correct? What functionality does the END OF MEMBER line provide? END OF MEMBER ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/… – Michael Petch Jul 2 at 3:33 @MichaelPetch Good pointer to doc, but the BMS map is neither a copybook nor a macro so the "END OF MEMBER" might just be left over code from someone copying from a copybook or a macro? – Michael Potter Jul 2 at 3:37 ...

Continuation of Comment Line

Continuation of Comment Line I am parsing some IBM Assembly Language which also happens to be a BMS map. The code looks like this: *02LN DFHMDF POS=(04,05),LENGTH=70,ATTRB=(ASKIP,BRT), X HILIGHT=REVERSE,COLOR=BLUE Note that the first line is a comment based on the * in the first column. Is the second line also a comment because the first line has a continuation character? The context of this question is that I am parsing the code without a mainframe as I am converting these maps to a different system. 1 Answer 1 As per the HLASM reference: Comment statements must lie within the statement field. If the comment extends into the continuation-indicator field, the statement following the comment statement is considered a continuation line of that comment statement. So your guess is right: you can also create multiline-comments by using the continuation-indicator. ...

Using ld to link a file makes it too big for a boot loader, works in nasm though :(

Using ld to link a file makes it too big for a boot loader, works in nasm though :( I'm trying to make a simple bootloader, but running into issues with ld (I think). When I compile my assembly file (below) with nasm -f bin , it works and I get a nice 512 byte file. For that one I include org 0x7c00 at the top and everything works as expected. nasm -f bin However, now I'm trying to do something a bit more complicated and link in a C kernel (unclear if I'm on the right path there, but I'm sure I'll learn that soon). Anyway, when I compile it with nasm -f elf -o loader_elf bootloader.asm and link it with i386-elf-ld loader_elf -o loader_exe -Ttext 0x7C00 , I get a file that is 4196 bytes (with a 1152 byte elf file). nasm -f elf -o loader_elf bootloader.asm i386-elf-ld loader_elf -o loader_exe -Ttext 0x7C00 What do I need to do for either the elf file or ld to get an executable with a proper file size? Thanks for your help! Here's the file in question: bits 16 ; 1...

Does any of current C++ compilers ever emit “rep movsb/w/d”?

Does any of current C++ compilers ever emit “rep movsb/w/d”? This question made me wonder, if current modern compilers ever emit REP MOVSB/W/D instruction. REP MOVSB/W/D Based on this discussion, it seems that using REP MOVSB/W/D could be beneficial on current CPUs. REP MOVSB/W/D But no matter how I tried, I cannot made any of the current compilers (GCC 8, Clang 7, MSVC 2017 and ICC 18) to emit this instruction. For this simple code, it could be reasonable to emit REP MOVSB : REP MOVSB void fn(char *dst, const char *src, int l) { for (int i=0; i<l; i++) { dst[i] = src[i]; } } But compilers emit a non-optimized simple byte-copy loop, or a huge unrolled loop (basically an inlined memmove ). Do any of the compilers use this instruction? memmove For that loop (without __restrict ), or ever? gcc has -mmemcpy-strategy=rep_4byte and -minline-all-stringops to override the tuning options. gcc.gnu.org/onlinedocs/gcc/x86-Options.html. ...

Stack in Assembly

Image
Stack in Assembly I need help with a code in Assembly. I'm coworking on this and my group just did this Assembly code where it supposed to do the same as what I did in #c. Could someone help me to understand what happens with de stack at least on the first steps so I can go on and conclude the rest? I'm a begginer in Assembly, but I know that these lines just save the value for the calling function, takes a frame for the called function and save space for local variables, but I can't figure out the next first steps. mov ebp mov ebp,esp sub esp, 16 Here is what I did in #c: void mult_integer(int X[A_Linhas][A_Colunas], int number) { int c, l; for (l = 0; l < A_Linhas; l++) { for (c = 0; c < A_Colunas; c++) { X[l][c] = number * X[l][c]; } } } And here is the code in Assembly: mul_integer: push ebp mov ebp, esp sub esp, 16 mov dword [ebp-4H], 0 jmp L_020 L_017: mov dword [ebp-8H], 0...