0

assembly

can anyone help me to understand this code assume ds:date, cs:cod date segment a db 0ffh b dw 0fffch s db 0,0,0 date ends cod segment start: mov ax, date mov ds, ax mov ax, b add al, a adc ah, 0 adc s + 2, 0 mov s, al mov s + 1, ah mov ax, 4c00h int 21h cod ends end start

25th Mar 2020, 6:12 PM
tibi
tibi - avatar
4 Respostas
+ 2
1 out of 2 date segment statement date ends is MASM grammar that defines a segment. https://docs.microsoft.com/en-us/cpp/assembler/masm/segment?view=vs-2019 In this case it just defines a segment that contains some data inside of it, the "date" is just the base address. The data within is layed out just like an array, contiguous, just like in C. Unlike C however, the data type are of variable size. The array is filled with the data 0xFF, 0xFFFC, 0x00, 0x00, 0x00, the labels 'a', 'b' and 's' are just pointers that point somewhere within this array to make it easier for us to work with the program, so you can just think of them as 'variables'. a: 0xFF b: 0xFFFC s: [0x00,0x00,0x00] Then the code segment starts mov ax, date Move the base address of the data segment "date" into ax mov ds, ax Move the base address into ds, a special register, you can't move directly into a special register so you have to use an intermediate register to move it, that's why the previous instruction is there. This special register is there so we can access physical memory outside of the range of 1 register, internally in the cpu is multiplies ds by 10h and then adds whatever other register to it. ds:ax would be ds * 10 + ax. This is a pain to work with in 16bit asm, tbh. Anyhow. mov ax, b Move the value 0xFFFC into ax. ( remember that b was pointing to it ) add al, a Add 0xFF to the lower part of ax. 0xFC + 0xFF = 0xFB + the carry flag set ( AX = 16 bit register, AH is the upper 8 bits, AL is the lower 8 bits ) adc ah, 0 Add 0 + the carry flag to AH. 0xFF + 0 + 1 = 0x00 + the carry flag set ( again ) adc s + 2, 0 Same as the previous operation. Remember that s is basically an array of 3 bytes, so this is the same as s[2] to access the last index. s[2] += 0 + carry flag so s[2] = 1 mov s, al Moves the value of al into s[0]. mov s + 1, ah Moves the value of ah into s[1].
25th Mar 2020, 7:25 PM
Dennis
Dennis - avatar
+ 1
2 out of 2 mov ax, 4c00h Moves 0x4C00 into ax int 21h Calls the interrupt 21 on the BIOS with AH = 0x4C ( thanks to the previous operation ) You can find a list of which function is which for the int 21 here: https://en.wikipedia.org/wiki/DOS_API 4Ch simply equals the function: Terminate with return code. So this entire program basically adds 2 numbers and stores the lower-byte, the upper-byte and the carry in memory.
25th Mar 2020, 7:27 PM
Dennis
Dennis - avatar
0
Thanks
26th Mar 2020, 12:38 PM
tibi
tibi - avatar